0

アクティビティを追加しようとしています。アプリケーションを作成しようとしています

<activity android:name=".About"
          android:label="@string/about_title"
          android:theme="@android:style/Theme.Dialog">            
</activity>

これを実行しましたが、アプリケーションを実行すると、エミュレーターからエラー メッセージが表示されます。メッセージは、「Sudoku アプリケーション (プロセス 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>

4

3 に答える 3

0

コード内で new_button を new_game_button に変更します XML では new_game_button と記述されていますが、コードでは new_button として使用しています

于 2013-08-03T10:41:17.310 に答える
0

20 行目のコードを確認してください。NullPointerException です。

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)
于 2013-08-02T01:26:55.363 に答える