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

标签 android android-intent

这是我的三项 Activity 代码!

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: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 run install again ,不会显示错误。
如果 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/

相关文章:

android - 访问 android.support.v4.app.FragmentPagerAdapter 中的 getString()?

Android MediaPlayer : Multiple sounds playing at the same time. 我的代码是

android - 打算从我的应用程序打开通知 channel 设置

java - 如果为空,则显示 imageview onclick 并在没有 imageview 的情况下发布

java - 使用PowerMock在android中单元测试单例类方法

java - 如何从 OpenGL ES 2.0 中的数组中读取 4 个以上的顶点数据?

Android putSerializable 与 putString

android - 如何在 ListView 中使用 Intent

java - 如何保持列表项和数据库行之间的连接?

java - 为什么在按下按钮后 setVisibility() 不让我的按钮和布局可见?