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));
}
# 1 楼答案
首先用以下方法替换
explainCellMembrane
方法:其次,尝试清理和重建项目,然后分享问题中的
logCat
编辑: 检查完你的
logCat
后,我可以看到错误不在你的logCat
中,但它在设备中,因此请尝试在你的模拟器或移动设备中删除应用程序,然后通过Android Studio再次安装,这对我来说也很有效,因为它经常随机发生# 2 楼答案
我认为,你的
Intent
没有问题。问题是您缺少在Manifest
文件中添加MembraneActivity
,该文件将位于<activity><activity/>
标记下。比如说,无论何时在应用程序中添加新的}识别您试图启动的
Activity
,都必须在Manifest
文件中添加Activity
名称。否则,您的应用程序将无法通过Intent
{Activity
现在在
Manifest
文件中添加MembraneActivity
之后,它将是这样的# 3 楼答案
您需要将该类添加到清单中
添加以下内容
这将放在
</activity>
标记下