android - setContentView(R.layout.activity_main) 导致应用程序崩溃

标签 android xml crash setcontentview

为 android 制作一个计算器程序,我有一个 xml 文件,其中包含编写该程序所需的所有按钮。它出现在 android studio 的设计部​​分,但是当我尝试在 android 手机(不是模拟器;它在我的电脑上运行太慢)上运行我的应用程序时,它只会导致它崩溃。也不知道为什么。

这是我的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<layout>
    <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnCount="4"
        android:rowCount="7"
        tools:context="com.example.pvtboromeo.calc.MainActivity">

        <EditText
            android:id="@+id/editText"
            android:background="@android:color/black"
            android:textColor="#FFFFFF"
            android:layout_row="0"
            android:layout_column="0"
            android:layout_columnSpan="4"
            android:layout_rowSpan="2"
            android:layout_rowWeight="2"
            android:layout_columnWeight="4"
            android:enabled="false"
            android:gravity="bottom|right"
            android:paddingRight="15sp"
            android:paddingBottom="15sp"
            android:textAlignment="gravity"
            android:textSize="40sp"/>

        <Button
            android:id="@+id/buttonC"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:layout_row="2"
            android:layout_rowWeight="1"
            android:background="@drawable/button_border"
            android:text="C"
            android:textSize="20sp" />

        <Button
            android:id="@+id/buttonNegative"
            android:layout_row="2"
            android:layout_column="1"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:background="@drawable/button_border"
            android:text="+/-"
            android:textSize="20sp"/>

        <Button
            android:id="@+id/buttonPercent"
            android:layout_row="2"
            android:layout_column="2"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:background="@drawable/button_border"
            android:text="%"
            android:textSize="20sp"/>

        <Button
            android:id="@+id/buttonDivide"
            android:layout_row="2"
            android:layout_column="3"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:background="@drawable/button_border2"
            android:text="/"
            android:textSize="20sp"/>

        <Button
            android:id="@+id/buttonSeven"
            android:layout_row="3"
            android:layout_column="0"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:background="@drawable/button_border"
            android:text="7"
            android:textSize="20sp" />

        <Button
            android:id="@+id/buttonEight"
            android:layout_row="3"
            android:layout_column="1"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:background="@drawable/button_border"
            android:text="8"
            android:textSize="20sp"/>

        <Button
            android:id="@+id/buttonNine"
            android:layout_row="3"
            android:layout_column="2"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:background="@drawable/button_border"
            android:text="9"
            android:textSize="20sp"/>

        <Button
            android:id="@+id/buttonMultiply"
            android:layout_row="3"
            android:layout_column="3"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:background="@drawable/button_border2"
            android:text="X"
            android:textSize="20sp"/>

        <Button
            android:id="@+id/buttonFour"
            android:layout_row="4"
            android:layout_column="0"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:background="@drawable/button_border"
            android:text="4"
            android:textSize="20sp" />

        <Button
            android:id="@+id/buttonFive"
            android:layout_row="4"
            android:layout_column="1"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:background="@drawable/button_border"
            android:text="5"
            android:textSize="20sp"/>

        <Button
            android:id="@+id/buttonSix"
            android:layout_row="4"
            android:layout_column="2"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:background="@drawable/button_border"
            android:text="6"
            android:textSize="20sp"/>

        <Button
            android:id="@+id/buttonSubtract"
            android:layout_row="4"
            android:layout_column="3"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:background="@drawable/button_border2"
            android:text="-"
            android:textSize="20sp"/>

        <Button
            android:id="@+id/buttonOne"
            android:layout_row="5"
            android:layout_column="0"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:background="@drawable/button_border"
            android:text="1"
            android:textSize="20sp" />

        <Button
            android:id="@+id/buttonTwo"
            android:layout_row="5"
            android:layout_column="1"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:background="@drawable/button_border"
            android:text="2"
            android:textSize="20sp"/>

        <Button
            android:id="@+id/buttonThree"
            android:layout_row="5"
            android:layout_column="2"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:background="@drawable/button_border"
            android:text="3"
            android:textSize="20sp"/>

        <Button
            android:id="@+id/buttonAdd"
            android:layout_row="5"
            android:layout_column="3"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:background="@drawable/button_border2"
            android:text="+"
            android:textSize="20sp"/>

        <Button
            android:id="@+id/buttonZero"
            android:layout_column="0"
            android:layout_columnSpan="2"
            android:layout_columnWeight="2"
            android:layout_row="6"
            android:layout_rowWeight="1"
            android:background="@drawable/button_border"
            android:text="0"
            android:textSize="20sp" />

        <Button
            android:id="@+id/buttonDecimal"
            android:layout_row="6"
            android:layout_column="2"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:background="@drawable/button_border"
            android:text="."
            android:textSize="20sp"/>

        <Button
            android:id="@+id/buttonEquals"
            android:layout_row="6"
            android:layout_column="3"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:background="@drawable/button_border2"
            android:text="="
            android:textSize="20sp"/>

    </GridLayout>
</layout>

这是我的 java 文件

package com.example.pvtboromeo.calc;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

如果没有 setContentView 代码,我的应用程序会运行,但不会显示任何内容(显然),但现在当我尝试运行它时,我的应用程序崩溃了。该错误只是说“不幸的是,Calc+ 已停止。”

我是 Android 开发新手。我错过了什么吗?我所有的语法似乎都很好。

注意:我还检查了是否导入了 android.R 而不是我的包 R,但它不存在...

编辑:所以我刚刚搞乱了一个测试项目,发现是 GridLayout 导致应用程序崩溃。我正在使用 android 5.1.1 (Lolipop) 运行,不确定这是否有帮助。我相信它是 api 22。有办法解决这个问题吗?

最佳答案

1) 配置您的应用程序以使用数据绑定(bind):

转到build.gradle(模块:app)并插入:

android {
    ....
     dataBinding { //line to insert
     enabled = true  //line to insert
}

}

1.1) 同步你的项目。

2) 在布局标签中插入:

xmlns:android="http://schemas.android.com/apk/res/android"

您的 xml 代码将如下所示:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <GridLayout
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnCount="4"
        android:rowCount="7"
        tools:context="com.example.pvtboromeo.calc.MainActivity">

3) 重建您的项目

4) 运行您的项目。

如果您想了解有关数据绑定(bind)的更多信息,请查看:https://developer.android.com/topic/libraries/data-binding/index.html

祝你好运!

这是我的代码:

构建.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "your_app_id"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}

主要 Activity :

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActivityMainBinding activityMainBinding = DataBindingUtil
                .setContentView(this, R.layout.activity_main);

    }
}

记住在使用 ActivityMainBinding 之前重建项目。

关于android - setContentView(R.layout.activity_main) 导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46474034/

相关文章:

Java 到 XML 的 Castor 映射

java - 治愈和预防 gephi 不稳定的提示和技巧?

MySQL 在大量行锁后崩溃

android - 在 Android 中从 HTML 生成位图

java - 我可以使用 SimpleXML 来解析结构未知的 XML 吗?

android - 我的应用程序即使已安装也没有出现在我的应用程序抽屉中

ruby - 获取没有文本节点的元素的子元素

c# - C#命令行脚本在Visual Studio中过早结束

android - 中心 View 位于另一个 View 下方

c++ - 在 Android NDK 的 C++ arm 中出现缩小转换错误