Android Studio两个按钮Java代码错误
下面是概要文件活动的java代码。我只做了大约30天,很多事情仍然让我困惑 当我运行模拟器时,它会打开,然后在第一个按钮后崩溃 它应该将用户发送到第二个活动 运行Logcat时,它挂在下面页面的第26行,表示它讨厌我的监听器中的按钮2。 帮助 Logcat文件如下:Android Studio两个按钮Java代码错误,java,android,Java,Android,下面是概要文件活动的java代码。我只做了大约30天,很多事情仍然让我困惑 当我运行模拟器时,它会打开,然后在第一个按钮后崩溃 它应该将用户发送到第二个活动 运行Logcat时,它挂在下面页面的第26行,表示它讨厌我的监听器中的按钮2。 帮助 Logcat文件如下: import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import and
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ProfileActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
// Button Click Goes to Main Activity
Button button1 = (Button) findViewById(R.id.start_over);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
back();
}
});
// Button Click Goes To Third Activity
Button button2 = (Button) findViewById(R.id.forward);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
forward();
}
});
}
private void back() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
private void forward() {
Intent intent = new Intent(this, ThirdActivity.class);
startActivity(intent);
}
}
MainActivity.XML如下:
FATAL EXCEPTION: main
Process: com.defy, PID: 2481
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.defy/com.defy.ProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.defy.ProfileActivity.onCreate(ProfileActivity.java:26)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ProfileActivity" />
<activity android:name=".ThirdActivity"></activity>
</application>
最后是下面的清单文件:
package com.defy.trailmaster;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Button Click Goes To Profile Activity
Button button = (Button) findViewById(R.id.start_hiking);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
hike();
}
});
}
private void hike() {
Intent intent = new Intent(this, ProfileActivity.class);
startActivity(intent);
}
}
配置文件活动XML如下:
FATAL EXCEPTION: main
Process: com.defy, PID: 2481
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.defy/com.defy.ProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.defy.ProfileActivity.onCreate(ProfileActivity.java:26)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ProfileActivity" />
<activity android:name=".ThirdActivity"></activity>
</application>
它很可能由于R.id.forward而崩溃,按钮2为空。 看起来您要么将按钮id命名为其他名称,要么错过了它
发布ProfileActivity的XML,这是您的问题。按钮2现在应该指徒步旅行
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_profile"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.defy.trailmaster.ProfileActivity">
<ImageView
android:id="@+id/background"
android:layout_width="0dp"
android:layout_height="0dp"
android:contentDescription="@string/app_name"
android:scaleType="centerCrop"
android:src="@drawable/background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<Button
android:id="@+id/start_over"
android:layout_width="95dp"
android:layout_height="35dp"
android:background="#000000"
android:text="@string/app_back"
android:textColor="#FDFEFE"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.145"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.966" />
<Button
android:id="@+id/hike_now"
android:layout_width="95dp"
android:layout_height="35dp"
android:background="#000000"
android:text="@string/app_hike"
android:textColor="#FDFEFE"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.868"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.966" />
<ImageView
android:id="@+id/tent"
android:layout_width="121dp"
android:layout_height="96dp"
android:contentDescription="@string/app_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.06"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.26"
app:srcCompat="@drawable/tent" />
<ImageView
android:id="@+id/left_hiker"
android:layout_width="92dp"
android:layout_height="90dp"
android:layout_marginBottom="0dp"
android:layout_marginStart="42dp"
android:layout_marginLeft="42dp"
android:layout_marginRight="8dp"
android:layout_marginEnd="8dp"
android:contentDescription="@string/app_name"
app:layout_constraintBottom_toTopOf="@+id/start_over"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/hiker_right"
app:srcCompat="@drawable/hiker_left" />
<ImageView
android:id="@+id/hiker_right"
android:layout_width="92dp"
android:layout_height="90dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="37dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/app_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.991"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.889"
app:srcCompat="@drawable/hiker_right"
tools:ignore="MissingConstraints,RtlHardcoded" />
</android.support.constraint.ConstraintLayout>
请将活动的布局xml添加到问题中。请同时发布xml。发布manifistpost日志输出。26号线在哪?Logcat是added@Stack_Master没问题!我最近开始为android开发,一开始也让我很沮丧。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_profile"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.defy.trailmaster.ProfileActivity">
<ImageView
android:id="@+id/background"
android:layout_width="0dp"
android:layout_height="0dp"
android:contentDescription="@string/app_name"
android:scaleType="centerCrop"
android:src="@drawable/background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<Button
android:id="@+id/start_over"
android:layout_width="95dp"
android:layout_height="35dp"
android:background="#000000"
android:text="@string/app_back"
android:textColor="#FDFEFE"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.145"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.966" />
<Button
android:id="@+id/hike_now"
android:layout_width="95dp"
android:layout_height="35dp"
android:background="#000000"
android:text="@string/app_hike"
android:textColor="#FDFEFE"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.868"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.966" />
<ImageView
android:id="@+id/tent"
android:layout_width="121dp"
android:layout_height="96dp"
android:contentDescription="@string/app_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.06"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.26"
app:srcCompat="@drawable/tent" />
<ImageView
android:id="@+id/left_hiker"
android:layout_width="92dp"
android:layout_height="90dp"
android:layout_marginBottom="0dp"
android:layout_marginStart="42dp"
android:layout_marginLeft="42dp"
android:layout_marginRight="8dp"
android:layout_marginEnd="8dp"
android:contentDescription="@string/app_name"
app:layout_constraintBottom_toTopOf="@+id/start_over"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/hiker_right"
app:srcCompat="@drawable/hiker_left" />
<ImageView
android:id="@+id/hiker_right"
android:layout_width="92dp"
android:layout_height="90dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="37dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/app_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.991"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.889"
app:srcCompat="@drawable/hiker_right"
tools:ignore="MissingConstraints,RtlHardcoded" />
</android.support.constraint.ConstraintLayout>
Final Button button2 = (Button) findViewById(R.id.hike_now);