Android Studio运行时错误-致命异常:主要“无法启​​动活动

时间:2019-09-19 22:26:52

标签: java android android-intent nullpointerexception runtime

我的滚动视图中有三个段落,每个段落需要在三个按钮之一上发生onclick事件后才可见。

我目前将它们设置为全部不可见。然后,我尝试在oncreate方法上将它们显示为可见。

但是我遇到了一个我不理解并且无法弄清的错误。而且我的代码中没有红色标记可以指定错误的来源。

PassageActivity.java

package com.example.threebuttons;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class PassageActivity extends AppCompatActivity {
    private TextView passage1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        passage1 = findViewById(R.id.passage_1);
        if (getIntent().hasExtra("passageNum") && getIntent().getExtras().getInt("passageNum") == 1){
            passage1.setVisibility(View.VISIBLE);
        }
        setContentView(R.layout.activity_passage);
    }
}

MainActivity.java

package com.example.threebuttons;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

    public void launchPassageOne(View view) {
        Intent intent = new Intent(this, PassageActivity.class);
        intent.putExtra("passageNum", 1);
        startActivity(intent) ;
    }

    public void launchPassageTwo(View view) {
        Intent intent = new Intent(this, PassageActivity.class);
        startActivity(intent) ;
    }

    public void launchPassageThree(View view) {
        Intent intent = new Intent(this, PassageActivity.class);
        startActivity(intent) ;
    }
}

XML文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".PassageActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/passage_1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:gravity="start|top"
                android:inputType="textMultiLine"
                android:text="@string/passage1"
                android:visibility="invisible"/>

            <EditText
                android:id="@+id/passage_2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:gravity="start|top"
                android:inputType="textMultiLine"
                android:text="@string/passage2"
                android:visibility="invisible"/>

            <EditText
                android:id="@+id/passage_3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:gravity="start|top"
                android:inputType="textMultiLine"
                android:text="@string/passage3"
                android:visibility="invisible"/>

        </LinearLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

错误消息

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.threebuttons, PID: 5356
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.threebuttons/com.example.threebuttons.PassageActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setVisibility(int)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setVisibility(int)' on a null object reference
        at com.example.threebuttons.PassageActivity.onCreate(PassageActivity.java:17)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

在按下相应按钮后,我需要使这些段落可见。但是我遇到了我无法弄清的错误。

我的代码执行不正确吗?因为我在android studio中的代码上看不到任何红色错误消息。

1 个答案:

答案 0 :(得分:-1)

在例外中查看此行:

{ "query": { "bool": { "must": [{ "bool": { "should": [{ "term": { "customerId": "123123123" } }, { "term": { "customerId": "5345345345" } }] } }, { "bool": { "should": [{ "match": { "location": "japan" } }] } }] } } }

我们可以看到passage_1为空。这可能是因为您将其作为XML中的EditText,而作为Java代码中的TextView。

edit :没关系。查看链接为重复的问题...