设置textview使应用程序崩溃

时间:2018-12-25 18:54:34

标签: java android textview

在此onClick方法中,我需要此textView更改为数组中的随机行之一,但是当我尝试使用此方法时,它仅显示“尝试调用虚拟方法'void android.widget.TextView.setText (java.lang.CharSequence)'引用到一个空对象引用上”并导致应用程序崩溃,这很奇怪,因为我的操作方式与以前相同,而上一个方法也可以正常工作,所以我在这里很迷失。

我已经尝试了在网上和其他地方找到的所有内容,因此希望您能对此提供帮助。

public void motivation(View v) {



    SK1 = (SeekBar) findViewById(R.id.seekBar);
    SK2 = (SeekBar) findViewById(R.id.seekBar3);
    SK3 = (SeekBar) findViewById(R.id.seekBar4);

    SK1.getProgress();
    SK2.getProgress();
    SK3.getProgress();
    quote = (TextView) findViewById(R.id.quot);

    int randomI = new Random().nextInt(quotes.length);
    String randomQuote = quotes[randomI];


    if (SK1.getProgress()>80 && SK2.getProgress()>80 && 
    SK3.getProgress()>80){

        setContentView(R.layout.motivated);
        quote.setText(randomQuote);

    } else {
        setContentView(R.layout.motivated);

        quote.setText("Keep working on it - me");
    }

}

以及文本视图的xml

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/cl">

<TextView
    android:id="@+id/quot"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:text="@string/text_view"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
 </android.support.constraint.ConstraintLayout>

和我遇到的错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mojefajne.test, PID: 8516
java.lang.IllegalStateException: Could not execute method for 
android:onClick
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    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.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
    at android.view.View.performClick(View.java:6597) 
    at android.view.View.performClickInternal(View.java:6574) 
    at android.view.View.access$3100(View.java:778) 
    at android.view.View$PerformClick.run(View.java:25885) 
    at android.os.Handler.handleCallback(Handler.java:873) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    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.setText(java.lang.CharSequence)' on a null object reference
    at com.example.mojefajne.test.MainActivity.motivation(MainActivity.java:100)
    at java.lang.reflect.Method.invoke(Native Method) 
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
    at android.view.View.performClick(View.java:6597) 
    at android.view.View.performClickInternal(View.java:6574) 
    at android.view.View.access$3100(View.java:778) 
    at android.view.View$PerformClick.run(View.java:25885) 
    at android.os.Handler.handleCallback(Handler.java:873) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    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)

MainActivity.java:100当然是setText命令。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我认为您已将包含textView的布局传递给了激励方法。如果我是对的,那么您需要找到textView为:quote =(TextView)v.findViewById(R.id.quot);