我正在Android Studio中开展多项活动的应用,我正在尝试将button1转到 SecondActivity ,将button2转到我的 < EM> ThirdActivity 即可。每当我在我的应用程序中运行按钮时,它都会崩溃。
这是我的 MainActivity.java:
package hobby_pc.medispeak;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick(View v) {
goToSecondActivity();
}
});
button2.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick(View v) {
goToThirdActivity();
}
});
}
private void goToSecondActivity()
{
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
private void goToThirdActivity()
{
Intent intent = new Intent(this, ThirdActivity.class);
startActivity(intent);
}
这是我的 activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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="hobby_pc.medispeak.MainActivity"
android:background="#ffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Welkom"
android:textSize="@dimen/abc_text_size_title_material_toolbar"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Medicijnenzoeken"
android:id="@+id/button1"
android:layout_marginTop="111dp"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Goochempie"
android:id="@+id/button2"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp" />
</RelativeLayout>
这是我的 AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="hobby_pc.medispeak">
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".SplashScreen"
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=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="com.Medispeak.androidsplashscreenexample.MAINACTIVITY" />
</intent-filter>
</activity>
<category android:name="android.intent.category.DEFAULT" />
<activity android:name=".ThirdActivity" />
<activity android:name=".SecondActivity" />
<receiver android:name=".AlarmReceiver"></receiver>
</application>
这是我崩溃时遇到的错误:
06-02 14:13:25.900 21384-21384/hobby_pc.medispeak E/AndroidRuntime: FATAL EXCEPTION: main
Process: hobby_pc.medispeak, PID: 21384
java.lang.RuntimeException: Unable to start activity ComponentInfo{hobby_pc.medispeak/hobby_pc.medispeak.ThirdActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2702)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at hobby_pc.medispeak.ThirdActivity.onCreate(ThirdActivity.java:34)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2655)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
答案 0 :(得分:0)
您似乎未在 AndroidManifest.xml 中声明 ThirdActivity 。
答案 1 :(得分:0)
你可以在这一行看到崩溃原因:java.lang.RuntimeException:无法启动活动ComponentInfo {hobby_pc.medispeak / hobby_pc.medispeak.ThirdActivity}:java.lang.NullPointerException:尝试调用虚方法&#39 ; void android.widget.TextView.setText(java.lang.CharSequence)&#39;在空对象引用上
我认为在thirdActivity中的onCreate方法中,您正在尝试为未绑定查看的textView设置setText。 在textView
上调用setText()之前使用findViewById()答案 2 :(得分:0)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at hobby_pc.medispeak.ThirdActivity.onCreate(ThirdActivity.java:34)
你的问题是,你的NullPointerException
课程中的ThirdActivity
位于第34行。您显然在setText()
上调用了TextView
而未使用findViewById(R.id.some_id)
进行初始化第一