1

这是我的三项活动代码!

主要的.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:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

这是布局e2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <Button
        android:id="@+id/btnLog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />


    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />

    </EditText>

</LinearLayout>

这是布局e3.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

和活动课

public class AbcActivity extends Activity {
            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                Intent myinten=new Intent(this,E2.class);
                startActivityForResult(myinten,0);

            }
        }
    public class E2 extends Activity {
          @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.e2);
                Button btn=(Button) findViewById(R.id.btnLog);
                btn.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                          Intent myinten=new Intent(E2.this,E3.class);
                          startActivityForResult(myinten,0);
                    }
                });
            }

    }

这是 androidmanifest.xml 中的代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="vn.daitran"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AbcActivity"
            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=".E2"
            android:label="@string/app_name" >           
        </activity>
         <activity
            android:name=".E3"
            android:label="@string/app_name" >           
        </activity>
    </application>

</manifest>

每次运行安装再次在 logcat 中显示错误

07-23 10:32:04.169: D/AndroidRuntime(1769): Shutting down VM
07-23 10:32:04.178: W/dalvikvm(1769): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
07-23 10:32:04.218: E/AndroidRuntime(1769): FATAL EXCEPTION: main
07-23 10:32:04.218: E/AndroidRuntime(1769): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException
07-23 10:32:04.218: E/AndroidRuntime(1769):     at android.app.LoadedApk.makeApplication(LoadedApk.java:482)
07-23 10:32:04.218: E/AndroidRuntime(1769):     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3938)
07-23 10:32:04.218: E/AndroidRuntime(1769):     at android.app.ActivityThread.access$1300(ActivityThread.java:123)
07-23 10:32:04.218: E/AndroidRuntime(1769):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1185)
07-23 10:32:04.218: E/AndroidRuntime(1769):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 10:32:04.218: E/AndroidRuntime(1769):     at android.os.Looper.loop(Looper.java:137)
07-23 10:32:04.218: E/AndroidRuntime(1769):     at android.app.ActivityThread.main(ActivityThread.java:4424)
07-23 10:32:04.218: E/AndroidRuntime(1769):     at java.lang.reflect.Method.invokeNative(Native Method)
07-23 10:32:04.218: E/AndroidRuntime(1769):     at java.lang.reflect.Method.invoke(Method.java:511)
07-23 10:32:04.218: E/AndroidRuntime(1769):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-23 10:32:04.218: E/AndroidRuntime(1769):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-23 10:32:04.218: E/AndroidRuntime(1769):     at dalvik.system.NativeStart.main(Native Method)
07-23 10:32:04.218: E/AndroidRuntime(1769): Caused by: java.lang.NullPointerException
07-23 10:32:04.218: E/AndroidRuntime(1769):     at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:362)
07-23 10:32:04.218: E/AndroidRuntime(1769):     at android.app.LoadedApk.getClassLoader(LoadedApk.java:305)
07-23 10:32:04.218: E/AndroidRuntime(1769):     at android.app.LoadedApk.makeApplication(LoadedApk.java:474)
07-23 10:32:04.218: E/AndroidRuntime(1769):     ... 11 more

注意:如果 AbcActivity -> E1 run install again ,不会显示错误。如果 AbcActivity -> E1 ->E2 再次运行安装,显示错误。

4

1 回答 1

1

我相信我发现了你的问题。

public class AbcActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent myinten=new Intent(this,E2.class);
        startActivityForResult(myinten,0);

        --->Button btn=(Button) findViewById(R.id.btn1);<--- // Remove this line
    }
}

您的 main.xml 没有 id 为 btn1 的按钮,这将导致 nullPointerException。您可以删除上面的行或在 main.xml 文件中创建一个 id 为 btn1 的按钮。

于 2012-07-23T04:26:03.977 回答