gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:06:17 27 4
gpt4 key购买 nike

这是我的三个 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com