【问题标题】:Android application crashes at activity change, possibly affected by layoutAndroid 应用程序在活动更改时崩溃,可能受布局影响
【发布时间】:2015-12-16 22:52:32
【问题描述】:

所以这真的很奇怪,我无法弄清楚。

我正在 Eclipse 上开发一个普通的教育应用程序,它只是应该有一些练习。

一切进展顺利,我完成了一个练习,然后我决定添加第二个练习(在一项新活动中,就像第一个一样)

所以我添加了一个新活动 (Exercise3),其中没有任何内容,只有 eclipse 添加的默认文本视图,我创建了一个按钮,从我的主菜单中指向该活动。

代码是这样的,与我的第一个练习完全一样的代码(当然不同的名字)和完美地改变了形式。

button2 = (Button) findViewById(R.id.button2);

        // Capture button clicks
        button2.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {

                // Start NewActivity.class
                Intent myIntent = new Intent(MainMenu.this,
                        Exercise3.class);
                startActivity(myIntent);
            }
        });

日志中绝对没有错误,应用程序运行完美,但是当我按下按钮 2 打开我创建的新活动时,我的应用程序崩溃了。 同样,eclipse 什么也没显示,代码与我的第一个活动按钮完全相同,效果很好,但我不知道这个按钮发生了什么。

不过,只有一些奇怪的地方,在新活动中,我添加了始终存在的标准“Hello World”文本视图,替换为“加载错误”。在我忘记了标准文本视图的主菜单活动中,现在它还显示“加载错误”而不是“hello world”。 (刚刚从 strings.xml 中检查了这是由我的合作伙伴在此应用中完成的)。

xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.main.pirateisland.Exercise3" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

我注意到文本视图中缺少 id,我尝试手动添加它,或者完全删除它,同样的问题,当我尝试从我的按钮转到该表单时应用程序崩溃。

绝对没有任何来自 eclipse 的 error.warning 让我很困惑。

我猜xml有问题,但我找不到什么。也许我在没有注意到的情况下改变了一些东西?

还尝试清理项目,重新启动eclipse等,没有任何帮助,问题仍然存在。

编辑:添加 manifest.xml :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.main.pirateisland"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="13"
        android:targetSdkVersion="22" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainMenu"
            android:label="@string/app_name"
            android:theme="@style/MyTheme" >
            <intent-filter>
                <action android:name="com.main.pirateisland.MainMenu" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".LoginScreen"
            android:label="@string/title_activity_login_screen"
            android:theme="@style/MyTheme" >
            <intent-filter android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Exercise1"
            android:label="@string/title_activity_exercise1"
            android:theme="@style/MyTheme" >
        </activity>
        <activity
            android:name=".SplitActivity"
            android:configChanges="keyboardHidden|orientation"
            android:label="@string/title_activity_split"
            android:screenOrientation="portrait"
            android:theme="@style/MyTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:label="@string/title_activity_main"
            android:screenOrientation="landscape"
            android:theme="@style/MyTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Exercise3"
            android:label="@string/title_activity_exercise3" >
        </activity>
    </application>

</manifest>

【问题讨论】:

  • 您是否将新活动添加到 AndroidManifest.xml 文件中?你能添加你的清单文件吗?

标签: android xml eclipse layout textview


【解决方案1】:

更改活动将更改的上下文。

Intent myIntent = new Intent(SecondActivity.this, Exercise3.class);

或者

Intent myIntent = new Intent(getApplicationContext(), Exercise3.class);

【讨论】:

    【解决方案2】:

    从.MainMenu的intent filter中移除action属性&lt;action android:name="com.main.pirateisland.MainMenu" /&gt;

    <activity
            android:name=".MainMenu"
            android:label="@string/app_name"
            android:theme="@style/MyTheme" >
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
    </activity>
    

    并将类别DEFAULT意图过滤器添加到.Exercise3

    <activity
          android:name=".Exercise3"
          android:label="@string/title_activity_exercise3" >
          <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
          </intent-filter>
    </activity>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多