有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java应用程序在试图启动名为的活动时崩溃

我是一个使用Android Studio创建应用程序的初学者

在我的主页/ActivityMain上,我希望放置一个文本视图,单击该视图后,将启动另一个RelativeLayout活动

我曾尝试使用intent来完成此操作,但当单击文本框时,应用程序崩溃。 请注意,对于所有代码,我认为不相关的语句都被删除(导入、捆绑) 下面是我的活动代码。xml:

<ImageView
    安卓:id="@+id/imageView2"
    安卓:layout_width="match_parent"
    安卓:layout_height="256dp"
    安卓:layout_margin="16dp" />

<TextView
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:text="Organelles" />

<TextView
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:id="@+id/membrane"
    安卓:text="Cell Membrane"
    安卓:onClick="explainCellMembrane" />

下面是我的MainActivity代码。java,包含包含我的意图的代码:

public class MainActivity extends AppCompatActivity {

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

        public void explainCellMembrane(View view) {
            Intent myIntent = new Intent(MainActivity.this, MembraneActivity.class);
            MainActivity.this.startActivity(myIntent);
        }
}

下面是intent启动的类(MembraneActivity)的代码:

    package com.example.安卓.cellularbiology;

public class MembraneActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_membrane);
    }

}

最后是我的自定义Java类布局代码:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    <TextView
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent"
        安卓:text="Membrane Explanation Page"/>

</LinearLayout>

在AndroidStudio中没有错误(红色下划线) 当应用程序崩溃时,它不会显示错误,只是说它已停止

我如何确定我的意图?还是应该使用另一种方法

谢谢你的帮助

Android清单:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    package="com.example.安卓.cellularbiology">

    <application
        安卓:allowBackup="true"
        安卓:icon="@mipmap/ic_launcher"
        安卓:label="@string/app_name"
        安卓:roundIcon="@mipmap/ic_launcher_round"
        安卓:supportsRtl="true"
        安卓:theme="@style/AppTheme">
        <activity 安卓:name=".MainActivity">
            <intent-filter>
                <action 安卓:name="安卓.intent.action.MAIN" />

                <category 安卓:name="安卓.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

日志:

    01/03 00:06:12: Launching app
No apk changes detected since last installation, skipping installation of /Users/FideronTSANG/Desktop/CellularBiology/app/build/outputs/apk/app-debug.apk
$ adb shell am force-stop com.example.安卓.cellularbiology
$ adb shell am start -n "com.example.安卓.cellularbiology/com.example.安卓.cellularbiology.MainActivity" -a 安卓.intent.action.MAIN -c 安卓.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Waiting for process to come online
Connected to process 24150 on device samsung-sm_c9000-b9dec0e1
Application terminated.

结论: 谢谢大家的回答和帮助! 你完全救了我的计划! 我希望你有一个愉快的一天:)

在我添加了

<activity
    安卓:name = ".MembraneActivity">
</activity>

到我的安卓清单

我还使用了

public void explainCellMembrane(View view) {

    startActivity(new Intent(MainActivity.this, MembraneActivity.class));

}

共 (3) 个答案

  1. # 1 楼答案

    首先用以下方法替换explainCellMembrane方法:

    public void explainCellMembrane(View view) {
    
            startActivity(new Intent(MainActivity.this, MembraneActivity.class));
    
        }
    

    其次,尝试清理和重建项目,然后分享问题中的logCat

    编辑: 检查完你的logCat后,我可以看到错误不在你的logCat中,但它在设备中,因此请尝试在你的模拟器或移动设备中删除应用程序,然后通过Android Studio再次安装,这对我来说也很有效,因为它经常随机发生

  2. # 2 楼答案

    我认为,你的Intent没有问题。问题是您缺少在Manifest文件中添加MembraneActivity,该文件将位于<activity><activity/>标记下。比如说,

    <activity
        android:name = ".MembraneActivity">
    </activity>
    

    无论何时在应用程序中添加新的Activity,都必须在Manifest文件中添加Activity名称。否则,您的应用程序将无法通过Intent{}识别您试图启动的Activity

    现在在Manifest文件中添加MembraneActivity之后,它将是这样的

    <?xml version="1.0" encoding="utf-8"?>
        <manifest  
            xmlns:android="http://schemas.android.com/apk/res/android"
            package="com.example.android.cellularbiology">
    
            <application
                android:allowBackup="true"
                android:icon="@mipmap/ic_launcher"
                android:label="@string/app_name" 
                android:roundIcon="@mipmap/ic_launcher_round"
                android:supportsRtl="true"
                android:theme="@style/AppTheme">
    
                <activity 
                    android:name=".MainActivity">
    
                    <intent-filter>
                        <action 
                            android:name="android.intent.action.MAIN" />
                        <category
                            android:name="android.intent.category.LAUNCHER" />
                    </intent-filter>
    
                </activity>
    
                <! your missing activity added here >
                <activity
                    android:name = ".MembraneActivity">
                </activity>
    
            </application>
        </manifest>
    
  3. # 3 楼答案

    您需要将该类添加到清单中

    添加以下内容

    <activity android:name=".MembraneActivity"></activity>
    

    这将放在</activity>标记下