我正在尝试添加一个活动。我正在尝试创建一个应用程序
<activity android:name=".About"
android:label="@string/about_title"
android:theme="@android:style/Theme.Dialog">
</activity>
我这样做了,但是当我运行我的应用程序时,我的模拟器给了我错误消息。消息是“抱歉应用程序数独(进程 org.example.Sudoku)已意外停止。请重试”。日志中显示错误 cat
08-02 07:25:51.093: D/AndroidRuntime(270): Shutting down VM
08-02 07:25:51.093: W/dalvikvm(270): threadid=1: thread exiting with uncaught exception
(group=0x4001d800)
08-02 07:25:51.153: E/AndroidRuntime(270): FATAL EXCEPTION: main
08-02 07:25:51.153: E/AndroidRuntime(270): java.lang.RuntimeException: Unable to start
activity ComponentInfo{org.example.sudoku/org.example.sudoku.Sudoku}:
java.lang.NullPointerException
08-02 07:25:51.153: E/AndroidRuntime(270): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-02 07:25:51.153: E/AndroidRuntime(270): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-02 07:25:51.153: E/AndroidRuntime(270): at
android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-02 07:25:51.153: E/AndroidRuntime(270): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-02 07:25:51.153: E/AndroidRuntime(270): at
android.os.Handler.dispatchMessage(Handler.java:99)
08-02 07:25:51.153: E/AndroidRuntime(270): at android.os.Looper.loop(Looper.java:123)
08-02 07:25:51.153: E/AndroidRuntime(270): at
android.app.ActivityThread.main(ActivityThread.java:4627)
08-02 07:25:51.153: E/AndroidRuntime(270): at
java.lang.reflect.Method.invokeNative(Native Method)
08-02 07:25:51.153: E/AndroidRuntime(270): at
java.lang.reflect.Method.invoke(Method.java:521)
08-02 07:25:51.153: E/AndroidRuntime(270): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-02 07:25:51.153: E/AndroidRuntime(270): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-02 07:25:51.153: E/AndroidRuntime(270): at dalvik.system.NativeStart.main(Native
Method)
08-02 07:25:51.153: E/AndroidRuntime(270): Caused by: java.lang.NullPointerException
08-02 07:25:51.153: E/AndroidRuntime(270): at
org.example.sudoku.Sudoku.onCreate(Sudoku.java:20)
08-02 07:25:51.153: E/AndroidRuntime(270): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-02 07:25:51.153: E/AndroidRuntime(270): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-02 07:25:51.153: E/AndroidRuntime(270): ... 11 more
我的 sudoku.java 文件代码:
public class Sudoku extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener((OnClickListener) this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener((OnClickListener) this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener((OnClickListener) this);
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
}
}
这是 main.xml 文件。
android:background="@color/background"
android:padding="30dip"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center">
<TextView
android:text="@string/main_title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="25dip"
android:textSize="24.5sp" />
<Button
android:id="@+id/continue_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/continue_label" />
<Button
android:id="@+id/new_game_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/new_game_label" />
<Button
android:id="@+id/about_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/about_label" />
<Button
android:id="@+id/exit_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/exit_label" />
</LinearLayout>