我有一个使用 IntelliJ 12 Community Edition IDE 的 Android 程序。我需要在使用按钮制作的不同布局 (.xml) 之间导航。但是,每当我使用模拟器运行它时,它只会打开 main.xml 屏幕,每当我单击按钮时,它都会显示:“不幸的是,ITax 已停止工作。”
这是我的 MyActivity.java 类的代码:
package com.example.ITax;
import android.app.Activity;
import android.content.Intent;
android.os.Bundle;
android.view.View;
import android.widget.Button;
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)findViewById(R.id.btn_info);
Button button2 = (Button)findViewById(R.id.btn_tutorial);
Button button3 = (Button)findViewById(R.id.btn_calc);
Button button4 = (Button)findViewById(R.id.btn_back_from_calcu);
Button button5 = (Button)findViewById(R.id.btn_next_from_calcu);
Button button6 = (Button)findViewById(R.id.btn_back_from_monthlyorannual);
Button button7 = (Button)findViewById(R.id.btn_next_from_monthlyorannual);
Button button8 = (Button)findViewById(R.id.btn_back_from_civilstatus);
Button button9 = (Button)findViewById(R.id.btn_next_from_civilstatus);
Button button10 = (Button)findViewById(R.id.btn_back_from_inputamount_monthly);
Button button11 = (Button)findViewById(R.id.btn_compute_from_monthly);
Button button12 = (Button)findViewById(R.id.btn_back_from_outputamount_monthly);
Button button13 = (Button)findViewById(R.id.btn_home_from_outputamount_monthly);
}
public void Open_BasicInfo()
{
Intent i = new Intent(this, OpenBasicInfo.class);
startActivity(i);
}
public void Open_Tutorial()
{
Intent i = new Intent(this, OpenTutorial.class);
startActivity(i);
}
public void Open_Calculator()
{
Intent i = new Intent(this, OpenCalculator.class);
startActivity(i);
}
public void Open_MonthlyOrAnnually()
{
Intent i = new Intent(this, OpenMonthlyOrAnnually.class);
startActivity(i);
}
public void Open_CivilStatus()
{
Intent i = new Intent(this, OpenCivilStatus.class);
startActivity(i);
}
public void Open_InputAmountsFromMonthly()
{
Intent i = new Intent(this, OpenInputAmountsFromMonthly.class);
startActivity(i);
}
public void Open_OutputAmountsFromMonthly()
{
Intent i = new Intent(this, OutputAmountsFromMonthly.class);
startActivity(i);
}
public void Open_Main()
{
Intent i = new Intent(this, MyActivity.class);
startActivity(i);
}
}
我已经为每个布局创建了一个类,并在 Android 清单中声明如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ITax"
android:versionCode="1"
android:versionName="ITax 1.0">
<uses-sdk android:minSdkVersion="1"/>
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name=".MyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".OpenCalculator">
</activity>
<activity android:name=".OpenCivilStatus">
</activity>
<activity android:name=".OpenInputAmountsFromMonthly">
</activity>
<activity android:name=".OutputAmountsFromMonthly">
</activity>
<activity android:name=".OpenTutorial">
</activity>
<activity android:name=".OpenBasicInfo">
</activity>
<activity android:name=".OpenMonthlyOrAnnually">
</activity>
<activity android:name=".OpenMain">
</activity>
</application>
</manifest>
我已经将代码 android:onClick = "name of method" 放在所有按钮中。
帮助!我在这里做错了什么?:(
每当我单击按钮时,模拟器中的应用程序就会停止工作。>.<