点击时关于按钮给出错误

时间:2012-03-01 11:44:14

标签: android

我为sudoku创建了一个about按钮,并在其上设置了OnClickListener,创建了名为About.java和about.xml的类,并在manifest.xml中添加了...........

About.java

package org.example.btg;

import android.app.Activity;
import android.os.Bundle;

public class About extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about);
    }
}

about.xml

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip" >
    <TextView
        android:id="@+id/about_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/about_text" />
</ScrollView>

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.example.btg"
    android:versionCode="1"
    android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".BrainTrainingGame"
        android:label="@string/app_name" >
    <activity android:name=".About"
        android:label="@string/about_title" >
    </activity>
         <intent-filter>
            <action android:name="android.intent.action.MAIN">
            </action>
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity> 
    </application>
</manifest>

btg.java

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;

出现错误时出现logcat如下:

03-01 12:16:51.062: D/AndroidRuntime(684): Shutting down VM
03-01 12:16:51.062: W/dalvikvm(684): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-01 12:16:51.126: E/AndroidRuntime(684): FATAL EXCEPTION: main
03-01 12:16:51.126: E/AndroidRuntime(684): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.btg/org.example.btg.BrainTrainingGame}: java.lang.NullPointerException
03-01 12:16:51.126: E/AndroidRuntime(684):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-01 12:16:51.126: E/AndroidRuntime(684):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-01 12:16:51.126: E/AndroidRuntime(684):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-01 12:16:51.126: E/AndroidRuntime(684):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-01 12:16:51.126: E/AndroidRuntime(684):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-01 12:16:51.126: E/AndroidRuntime(684):  at android.os.Looper.loop(Looper.java:123)
03-01 12:16:51.126: E/AndroidRuntime(684):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-01 12:16:51.126: E/AndroidRuntime(684):  at java.lang.reflect.Method.invokeNative(Native Method)
03-01 12:16:51.126: E/AndroidRuntime(684):  at java.lang.reflect.Method.invoke(Method.java:521)
03-01 12:16:51.126: E/AndroidRuntime(684):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-01 12:16:51.126: E/AndroidRuntime(684):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-01 12:16:51.126: E/AndroidRuntime(684):  at dalvik.system.NativeStart.main(Native Method)
03-01 12:16:51.126: E/AndroidRuntime(684): Caused by: java.lang.NullPointerException
03-01 12:16:51.126: E/AndroidRuntime(684):  at org.example.btg.BrainTrainingGame.onCreate(BrainTrainingGame.java:21)
03-01 12:16:51.126: E/AndroidRuntime(684):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-01 12:16:51.126: E/AndroidRuntime(684):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-01 12:16:51.126: E/AndroidRuntime(684):  ... 11 more
03-01 12:21:51.292: I/Process(684): Sending signal. PID: 684 SIG: 9

非常感谢帮助!

它仍无效。

1 个答案:

答案 0 :(得分:0)

您需要移动清单中的“关于”部分。您不能在另一个活动中定义活动:

<activity
  android:name=".BrainTrainingGame"
  android:label="@string/app_name" >

  <intent-filter>
    <action android:name="android.intent.action.MAIN">
    </action>
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>
<activity android:name=".About"
  android:label="@string/about_title" >
</activity>