我是Android开发的初学者。我最近学会了如何在项目中的多个活动之间切换。但是当我运行应用程序时,它总是停止工作。我尝试了尽可能多的方法:检查AndroidManifest.xml,检查是否存在区分大小写的错误,或者更改了AVD,但它仍然无法正常工作!
这是我的MainActivity.java:
package com.example.admin.test;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btnCallActivityB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCallActivityB = (Button) findViewById(R.id.btnCallActivityB);
btnCallActivityB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, ActivityB.class);
startActivity(intent);
}
});
}
}
这是我的activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/btnCallActivityB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Go to activity B" />
这是我的ActivityB.java:
package com.example.admin.test;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class ActivityB extends AppCompatActivity {
private Button btnCallActivityA;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCallActivityA = (Button) findViewById(R.id.btnCallActivityA);
btnCallActivityA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ActivityB.this, MainActivity.class);
startActivity(intent);
}
});
}
}
这是我的activity_b.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<Button
android:id="@+id/btnCallActivityA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Go to activity A" />
这是我的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admin.test">
<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>
<activity android:name=".ActivityB"></activity>
</application>
答案 0 :(得分:2)
请更改setContentView()
课程中的ActivityB
方法,如下所示。
setContentView(R.layout.activity_b)
答案 1 :(得分:1)
您应该将setContentView(R.layout.activity_main)
更改为setContentView(R.layout.activity_b)
中的ActivityB
。这将解决您的问题。
public class ActivityB extends AppCompatActivity {
private Button btnCallActivityA;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
btnCallActivityA = (Button) findViewById(R.id.btnCallActivityA);
btnCallActivityA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ActivityB.this, MainActivity.class);
startActivity(intent);
}
});
}
}
答案 2 :(得分:0)
您将setContentView(R.layout.activity_main);
放在ActivityB中。
将setContentView(R.layout.activity_main);
替换为setContentView(R.layout.activity_b)
。