我是照着Hello android 3rd这本书的程序运行的。代码如下:
Sudoku.java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.sudoku"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name" >
<activity android:name=".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>
</manifest>About.java
package org.example.sudoku;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);
}
}main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="30dip"
android:orientation="horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center">
<TextView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="25dip"
android:textSize="24.5sp"
android:text="@string/main_title" />
<Button
android:id="@+id/new_game_button"
android:text="@string/new_game_label"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<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/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>
</LinearLayout>about.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dip"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/about_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/about_text"/>
</ScrollView>AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.sudoku"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name" >
<activity android:name=".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>
</manifest>strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Sudoku</string>
<string name="main_title">Android Sudoku</string>
<string name="continue_label">Continue</string>
<string name="new_game_label">New Game</string>
<string name="about_label">About</string>
<string name="exit_label">Exit</string>
<string name="about_title">About Android Sudoku</string>
<string name="about_text">\
Sudoku is a logic-based number placement puzzle.
Starting with a partially completed 9x9 grid, the
objective is to fill the grid so that each
row, each column, and each of the 3x3 boxes
(also called <i>blocks</i>) contains the digits
1 to 9 exactly once.
</string>
</resources>错误提示:Installing Sudoku.apk...
[2011-09-11 12:07:35 - Sudoku] Success!
[2011-09-11 12:07:35 - Sudoku] Starting activity org.example.sudoku.Sudoku on device emulator-5554
[2011-09-11 12:07:36 - Sudoku] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=org.example.sudoku/.Sudoku }书上是说插入了上面红色那3行代码,就能够链接到about activity。
找了一个半天都不知道问题在哪。大神帮忙。!
Sudoku.java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.sudoku"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name" >
<activity android:name=".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>
</manifest>About.java
package org.example.sudoku;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);
}
}main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="30dip"
android:orientation="horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center">
<TextView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="25dip"
android:textSize="24.5sp"
android:text="@string/main_title" />
<Button
android:id="@+id/new_game_button"
android:text="@string/new_game_label"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<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/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>
</LinearLayout>about.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dip"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/about_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/about_text"/>
</ScrollView>AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.sudoku"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name" >
<activity android:name=".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>
</manifest>strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Sudoku</string>
<string name="main_title">Android Sudoku</string>
<string name="continue_label">Continue</string>
<string name="new_game_label">New Game</string>
<string name="about_label">About</string>
<string name="exit_label">Exit</string>
<string name="about_title">About Android Sudoku</string>
<string name="about_text">\
Sudoku is a logic-based number placement puzzle.
Starting with a partially completed 9x9 grid, the
objective is to fill the grid so that each
row, each column, and each of the 3x3 boxes
(also called <i>blocks</i>) contains the digits
1 to 9 exactly once.
</string>
</resources>错误提示:Installing Sudoku.apk...
[2011-09-11 12:07:35 - Sudoku] Success!
[2011-09-11 12:07:35 - Sudoku] Starting activity org.example.sudoku.Sudoku on device emulator-5554
[2011-09-11 12:07:36 - Sudoku] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=org.example.sudoku/.Sudoku }书上是说插入了上面红色那3行代码,就能够链接到about activity。
找了一个半天都不知道问题在哪。大神帮忙。!
package org.example.sudoku;import android.app.Activity;
import android.os.Bundle;import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;public class Sudoku extends Activity implements OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up click listeners for all the buttons
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(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那么多,要帖哪些呢?
09-12 12:58:54.471: ERROR/AndroidRuntime(409): FATAL EXCEPTION: main
09-12 12:58:54.471: ERROR/AndroidRuntime(409): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.sudoku/org.example.sudoku.Sudoku}: java.lang.NullPointerException
09-12 12:58:54.471: ERROR/AndroidRuntime(409): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-12 12:58:54.471: ERROR/AndroidRuntime(409): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-12 12:58:54.471: ERROR/AndroidRuntime(409): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-12 12:58:54.471: ERROR/AndroidRuntime(409): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-12 12:58:54.471: ERROR/AndroidRuntime(409): at android.os.Handler.dispatchMessage(Handler.java:99)
09-12 12:58:54.471: ERROR/AndroidRuntime(409): at android.os.Looper.loop(Looper.java:123)
09-12 12:58:54.471: ERROR/AndroidRuntime(409): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-12 12:58:54.471: ERROR/AndroidRuntime(409): at java.lang.reflect.Method.invokeNative(Native Method)
09-12 12:58:54.471: ERROR/AndroidRuntime(409): at java.lang.reflect.Method.invoke(Method.java:507)
09-12 12:58:54.471: ERROR/AndroidRuntime(409): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-12 12:58:54.471: ERROR/AndroidRuntime(409): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-12 12:58:54.471: ERROR/AndroidRuntime(409): at dalvik.system.NativeStart.main(Native Method)
09-12 12:58:54.471: ERROR/AndroidRuntime(409): Caused by: java.lang.NullPointerException
09-12 12:58:54.471: ERROR/AndroidRuntime(409): at org.example.sudoku.Sudoku.onCreate(Sudoku.java:20)
09-12 12:58:54.471: ERROR/AndroidRuntime(409): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-12 12:58:54.471: ERROR/AndroidRuntime(409): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-12 12:58:54.471: ERROR/AndroidRuntime(409): ... 11 more
好我将错误都贴出来了。楼上楼下的兄弟帮忙解答。
空指针错误!看看哪个对象或变量没有实例化或赋值
这里about应该是大写哦。2、你照贴代码,到底你明白不明白每段的意义是什么?这样贴代码,根本不能真正找到原因,看那个也眼累。
我不贴代码,logcat你们怎么帮我找错误呢。?
你是说R.java文件之中吗?都是有值的。
R.java/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/package org.example.sudoku;public final class R {
public static final class attr {
}
public static final class color {
public static final int background=0x7f040000;
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int about_button=0x7f060003;
public static final int about_content=0x7f060000;
public static final int continue_button=0x7f060001;
public static final int exit_button=0x7f060004;
public static final int new_button=0x7f060005;
public static final int new_game_button=0x7f060002;
}
public static final class layout {
public static final int about=0x7f030000;
public static final int main=0x7f030001;
}
public static final class string {
public static final int about_label=0x7f050004;
public static final int about_text=0x7f050007;
public static final int about_title=0x7f050006;
public static final int app_name=0x7f050000;
public static final int continue_label=0x7f050002;
public static final int exit_label=0x7f050005;
public static final int main_title=0x7f050001;
public static final int new_game_label=0x7f050003;
}
}
与书中自带的代码进行了多番对比,实在没有发现错误。难道是因为我与书中的实例使用了相同的package名吗?但是我之前一直都都正常运行啊。
用下面这种方式
Button newButton = (Button)findViewById(R.id.new_game_button);