gpt4 book ai didi

java - 应用程序意外停止。由 : Java. lang.NullPointerException 引起的错误

转载 作者:行者123 更新时间:2023-11-29 21:36:03 25 4
gpt4 key购买 nike

主.java

package com.learnactivities;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Main extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);

Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
startActivity(new Intent(Main.this, Second.class));

}
});
}

Activity .xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Main" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button1" />

</RelativeLayout>

第二个.java

package com.learnactivities;

import android.app.Activity;
import android.os.Bundle;

public class Second extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}

第二个.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Second" >

<TextView
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="15dp"
android:text="@string/nd" />

</RelativeLayout>

AndroidManifest.xml

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

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.learnactivities.Main"
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=".second" />
</application>

</manifest>

我收到以下错误。我可以看到由 java.lang.NullPointerException 引起的错误。我在这里和那里尽了最大的努力,但找不到解决方案。Plz,有人帮我解决这个问题吗?

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

最佳答案

R.id.button1second.xml 中声明,但您在 activity.xml 中查找它。所以在 Main 中,当你这样做的时候

   Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()

findViewById 返回 null

关于java - 应用程序意外停止。由 : Java. lang.NullPointerException 引起的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18427426/

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