【发布时间】:2014-12-21 12:41:10
【问题描述】:
你好,我正在从书中学习 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>
关于.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>
数独.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
}
}
}
关于.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
【问题讨论】:
-
@ShivankSagar 请发布逻辑错误
-
@shayanpourvatan 天哪,我只是对名称感到困惑。对不起,我删除了我的评论
-
这是启动错误...意味着您尚未在 xml 文件中定义任何
textview或button(或任何布局)。