Android应用程序显示错误(不幸的是,数独已经停止)

时间:2014-12-21 12:41:11

标签: java android xml

你好我正在从书你好,android学习android,我被困在第3章。 我试图在用户单击“关于”按钮时显示包含有关数独游戏信息的新活动 但是由于某种原因,当我运行应用程序时它不幸地告诉我,数独已停止。我试图自己解决但无法弄清楚问题。我需要你的hellllllllllp
这里的文件列在下面

的AndroidManifest.xml

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.android.sudoku.Sudoku"
        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=".About"
        android:label="@string/about_title">
    </activity>

</application>

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_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/about_text"/>

</ScrollView>

activity_sudoku.xml(我的主要活动)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:background="@color/background"
android:padding="30dip" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="25dip"
    android:text="@string/main_title"
    android:textSize="24.5sp" />

<Button
    android:id="@+id/continue_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/continue_label" />

<Button
    android:id="@+id/meow"
    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" />

<Button
    android:id="@+id/ext_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/dialog_label" />

</LinearLayout>

Sudoku.java

package com.android.sudoku;

//import android.support.v7.app.ActionBarActivity;
//import android.support.v7.app.ActionBar;
//import android.support.v4.app.Fragment;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.Bundle;
//import android.view.LayoutInflater;
//import android.view.Menu;
//import android.view.MenuItem;
import android.app.Activity;
//import android.view.View;
//import android.view.ViewGroup;
//import android.os.Build;

public class Sudoku extends Activity implements OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sudoku);

        // setup click listeners for all the button
        View continueButton = findViewById(R.id.continue_button);
        continueButton.setOnClickListener(this);

        View newButton = findViewById(R.id.new_button);
        newButton.setOnClickListener(this);
        System.out.print("here ");
        View aboutButton = findViewById(R.id.about_button);
        aboutButton.setOnClickListener(this);
        System.out.println("no error");
        View exitButton = findViewById(R.id.exit_button);
        exitButton.setOnClickListener(this);

        View dialogButton = findViewById(R.id.ext_button);
        dialogButton.setOnClickListener(this);

        if (savedInstanceState == null) {

        }
    }


    public void onClick(View v)  {

        switch(v.getId()) {

        case R.id.about_button:
            Intent i = new Intent(this,About.class);
            startActivity(i);
            break;
        // more buttons go here id any
        }

    }

}

About.java

package com.android.sudoku;
import android.app.Activity;
import android.os.Bundle;
public class About extends Activity {
protected void onCreate(Bundle savedInstanceState)  {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.about);
}
}

LogCat错误

12-21 08:03:36.452: E/AndroidRuntime(1415): FATAL EXCEPTION: main
12-21 08:03:36.452: E/AndroidRuntime(1415): Process: com.android.sudoku, PID: 1415
12-21 08:03:36.452: E/AndroidRuntime(1415): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.sudoku/com.android.sudoku.Sudoku}: java.lang.NullPointerException
12-21 08:03:36.452: E/AndroidRuntime(1415):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
12-21 08:03:36.452: E/AndroidRuntime(1415):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-21 08:03:36.452: E/AndroidRuntime(1415):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-21 08:03:36.452: E/AndroidRuntime(1415):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-21 08:03:36.452: E/AndroidRuntime(1415):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-21 08:03:36.452: E/AndroidRuntime(1415):     at android.os.Looper.loop(Looper.java:136)
12-21 08:03:36.452: E/AndroidRuntime(1415):     at android.app.ActivityThread.main(ActivityThread.java:5017)
12-21 08:03:36.452: E/AndroidRuntime(1415):     at java.lang.reflect.Method.invokeNative(Native Method)
12-21 08:03:36.452: E/AndroidRuntime(1415):     at java.lang.reflect.Method.invoke(Method.java:515)
12-21 08:03:36.452: E/AndroidRuntime(1415):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-21 08:03:36.452: E/AndroidRuntime(1415):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-21 08:03:36.452: E/AndroidRuntime(1415):     at dalvik.system.NativeStart.main(Native Method)
12-21 08:03:36.452: E/AndroidRuntime(1415): Caused by: java.lang.NullPointerException
12-21 08:03:36.452: E/AndroidRuntime(1415):     at com.android.sudoku.Sudoku.onCreate(Sudoku.java:30)
12-21 08:03:36.452: E/AndroidRuntime(1415):     at android.app.Activity.performCreate(Activity.java:5231)
12-21 08:03:36.452: E/AndroidRuntime(1415):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-21 08:03:36.452: E/AndroidRuntime(1415):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
12-21 08:03:36.452: E/AndroidRuntime(1415):     ... 11 more
12-21 08:03:42.062: I/Process(1415): Sending signal. PID: 1415 SIG: 9
12-21 08:04:17.702: D/AndroidRuntime(1455): Shutting down VM
12-21 08:04:17.702: W/dalvikvm(1455): threadid=1: thread exiting with uncaught exception (group=0xb2af0ba8)
12-21 08:04:17.732: E/AndroidRuntime(1455): FATAL EXCEPTION: main
12-21 08:04:17.732: E/AndroidRuntime(1455): Process: com.android.sudoku, PID: 1455
12-21 08:04:17.732: E/AndroidRuntime(1455): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.sudoku/com.android.sudoku.Sudoku}: java.lang.NullPointerException
12-21 08:04:17.732: E/AndroidRuntime(1455):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
12-21 08:04:17.732: E/AndroidRuntime(1455):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-21 08:04:17.732: E/AndroidRuntime(1455):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-21 08:04:17.732: E/AndroidRuntime(1455):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-21 08:04:17.732: E/AndroidRuntime(1455):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-21 08:04:17.732: E/AndroidRuntime(1455):     at android.os.Looper.loop(Looper.java:136)
12-21 08:04:17.732: E/AndroidRuntime(1455):     at android.app.ActivityThread.main(ActivityThread.java:5017)
12-21 08:04:17.732: E/AndroidRuntime(1455):     at java.lang.reflect.Method.invokeNative(Native Method)
12-21 08:04:17.732: E/AndroidRuntime(1455):     at java.lang.reflect.Method.invoke(Method.java:515)
12-21 08:04:17.732: E/AndroidRuntime(1455):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-21 08:04:17.732: E/AndroidRuntime(1455):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-21 08:04:17.732: E/AndroidRuntime(1455):     at dalvik.system.NativeStart.main(Native Method)
12-21 08:04:17.732: E/AndroidRuntime(1455): Caused by: java.lang.NullPointerException
12-21 08:04:17.732: E/AndroidRuntime(1455):     at com.android.sudoku.Sudoku.onCreate(Sudoku.java:30)
12-21 08:04:17.732: E/AndroidRuntime(1455):     at android.app.Activity.performCreate(Activity.java:5231)
12-21 08:04:17.732: E/AndroidRuntime(1455):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-21 08:04:17.732: E/AndroidRuntime(1455):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
12-21 08:04:17.732: E/AndroidRuntime(1455):     ... 11 more
12-21 08:04:21.882: I/Process(1455): Sending signal. PID: 1455 SIG: 9

2 个答案:

答案 0 :(得分:0)

我在您的布局中看不到ID为new_button的视图。因此findViewById(R.id.new_button)返回null并在null上调用方法会导致异常。

答案 1 :(得分:0)

而不是:

View newButton = findViewById(R.id.new_button);

使用它:

Button newButton = (Button) findViewById(R.id.new_button);