我使用的是android应用开发揭秘这本书,书中的源代码是1.5SDK下编写的,我的eclipse是3.72 SDK2.2。我把书中的工程导入后,修改SDK到2.2运行可以点击按钮进行两个页面的跳转,但是我自己在SDK2.2重写了一遍后,再运行就一直应用意外停止,昨天晚上就一直出现这个错误。我开始开以为是我代码写错了。然后我按照书中的元代复制了一遍,不管是AndroidManiFest.xml还是两个acitvity的源代码,以及string.xml main.xml main2.xml   结果用模拟器运行老是应用程序意外停止。然后我又用我的手机试了,还是意外停止。然后我注释掉跳转的那部分代码后,可以运行了。 但是为什么一样的代码我的就不行?我不知道我跳转的那部分代码哪里写错了。快崩溃了,各位帮忙快点解救我
Activity01.javapackage com.faith;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Activity01 extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button=(Button)findViewById(R.id.Button1);
button.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
/* 新建一个Intent对象 */
Intent intent = new Intent();
/* 指定intent要启动的类 */
intent.setClass(Activity01.this, Activity02.class);
/* 启动一个新的Activity */
startActivity(intent);
/* 关闭当前的Activity */
Activity01.this.finish();
}
});
}
}Activity02.java
package com.faith;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;public class Activity02 extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Button button = (Button) findViewById(R.id.Button2);
/* 监听button的事件信息 */
button.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
/* 新建一个Intent对象 */
Intent intent = new Intent();
/* 指定intent要启动的类 */
intent.setClass(Activity02.this, Activity01.class);
/* 启动一个新的Activity */
startActivity(intent);
/* 关闭当前的Activity */
Activity02.this.finish();
}
});
}
}main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >    <TextView
        android:name="@+id/TextView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/app_name"/>
<Button
    android:name="@+id/Button1"
    android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/exchange"/>
</LinearLayout>main2.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >    <TextView
        android:name="@+id/TextView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/app2_name"/>
<Button
    android:name="@+id/Button2"
    android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/exchange"/>
</LinearLayout>string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">01</string>
    <string name="app2_name">02</string>
    <string name="exchange">切换</string></resources>AndroidManiFest.xml<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.faith"
    android:versionCode="1"
    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="8" 
         android:targetSdkVersion="8"/>    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".Activity01"
            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="Activity02">
            
        </activity>
        
    </application></manifest>

解决方案 »

  1.   

    05-27 04:18:21.440: D/AndroidRuntime(511): Shutting down VM
    05-27 04:18:21.440: W/dalvikvm(511): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
    05-27 04:18:21.469: E/AndroidRuntime(511): FATAL EXCEPTION: main
    05-27 04:18:21.469: E/AndroidRuntime(511): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.faith/com.faith.Activity01}: java.lang.NullPointerException
    05-27 04:18:21.469: E/AndroidRuntime(511):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
    05-27 04:18:21.469: E/AndroidRuntime(511):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    05-27 04:18:21.469: E/AndroidRuntime(511):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    05-27 04:18:21.469: E/AndroidRuntime(511):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    05-27 04:18:21.469: E/AndroidRuntime(511):  at android.os.Handler.dispatchMessage(Handler.java:99)
    05-27 04:18:21.469: E/AndroidRuntime(511):  at android.os.Looper.loop(Looper.java:123)
    05-27 04:18:21.469: E/AndroidRuntime(511):  at android.app.ActivityThread.main(ActivityThread.java:4627)
    05-27 04:18:21.469: E/AndroidRuntime(511):  at java.lang.reflect.Method.invokeNative(Native Method)
    05-27 04:18:21.469: E/AndroidRuntime(511):  at java.lang.reflect.Method.invoke(Method.java:521)
    05-27 04:18:21.469: E/AndroidRuntime(511):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    05-27 04:18:21.469: E/AndroidRuntime(511):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    05-27 04:18:21.469: E/AndroidRuntime(511):  at dalvik.system.NativeStart.main(Native Method)
    05-27 04:18:21.469: E/AndroidRuntime(511): Caused by: java.lang.NullPointerException
    05-27 04:18:21.469: E/AndroidRuntime(511):  at com.faith.Activity01.onCreate(Activity01.java:17)
    05-27 04:18:21.469: E/AndroidRuntime(511):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    05-27 04:18:21.469: E/AndroidRuntime(511):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    05-27 04:18:21.469: E/AndroidRuntime(511):  ... 11 more
      

  2.   

    05-27 04:18:21.469: E/AndroidRuntime(511): at com.faith.Activity01.onCreate(Activity01.java:17) activity01第17行代码有问题。你的第十七行代码是啥?
      

  3.   

    button.setOnClickListener(new Button.OnClickListener()
    改为button.setOnClickListener(new OnClickListener()
      

  4.   

    空指针导致异常,再仔细检查代码,或者加非空判断。
    05-27 04:18:21.469: E/AndroidRuntime(511): Caused by: java.lang.NullPointerException
      

  5.   

    你的AndroidManiFest.xml文件中Activity02那有错,前面少了个点啊
      

  6.   

    应该是这样的<activity
                android:name=".Activity02">