[英]Setting textview crashes the application
In this onClick method I need this textView to change to one of the random lines from the array, but when I try to use this method, it just shows "Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference" and crashes the app, which is pretty weird because I did it the same way as before and the previous method works perfectly, so I'm pretty lost here. 在此onClick方法中,我需要此textView更改为数组中的随机行之一,但是当我尝试使用此方法时,它只是显示“尝试调用虚拟方法'void android.widget.TextView.setText(java。 lang.CharSequence)'插入到空对象引用上”并导致应用程序崩溃,这很奇怪,因为我的操作方式与以前相同,并且前一种方法也可以正常工作,因此我在这里很迷失。
I've tried everything I found here and in other places online, so I hope you could help me with that. 我已经尝试了在网上和其他地方找到的所有内容,因此希望您能对此提供帮助。
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");
}
}
as well as the xml of the text view 以及文本视图的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>
and the error I'm getting 和我得到的错误
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 being of course the setText command. MainActivity.java:100当然是setText命令。
Thanks for any help! 谢谢你的帮助!
I think you passed a layout containing a textView to a motivation method. 我认为您已将包含textView的布局传递给了激励方法。 If I am right then you need to find the textView as: quote = (TextView) v.findViewById(R.id.quot);
如果我是对的,那么您需要找到textView为:quote =(TextView)v.findViewById(R.id.quot);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.