android4.0 :Unable to instantiate application android. app.Application : java. lang.NullPointerException

标签 android android-intent

这是我的三个 Activity 的代码!

ma​​in.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>

和 Activity 类

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 再次运行安装,不会显示错误。 如果 AbcActivity -> E1 ->E2 再次运行安装,显示错误。

最佳答案

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

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 的按钮。

关于android4.0 :Unable to instantiate application android. app.Application : java. lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11606193/

相关文章:

尝试构建 React Native 0.59 应用程序时与 AndroidX 相关的 AndroidManifest 错误

android - 是否可以阻止外发短信?

java - 如何在android中重复 Activity ?

java - 如何在暂停时重新启动整个应用程序?

android - 如何确定我们来自哪个 Activity ?

android - 如何在 Android 上使用 Firestore 创建邀请系统

android - libmp3lame.a 中 undefined symbol _init_xrpow_core_init

java - "Automatically lock after n seconds"时未收到 ACTION_USER_PRESENT

android - onActivityResult 从未调用过

java - 如何在android中的外部存储中创建目录,将外部存储路径显示为 "storage/emulated/0"?