只有一個活動的簡單應用程序正在創建尋找佈局的TextView並顯示爲空的問題。請點擊提交按鈕,調試應用程序崩潰的原因。我在堆棧溢出中嘗試了許多解決方案,許多解決方案都表示該視圖可能位於指定的活動之外,但這裏只有一個活動在那裏。所以研究並沒有幫助我,現在我還剩下那些能夠預測我可能犯下的錯誤的開發人員。textView setText()空指針異常[單個活動應用程序]
MainActivity.java
package me.thirumurugan.quiz;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button submit;
TextView result;
RadioButton answer_a;
EditText answer_b;
CheckBox answer_c1, answer_c2, answer_c3, answer_c4;
EditText answer_d;
int correct = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = (TextView) findViewById(R.id.result);
answer_a = (RadioButton) findViewById(R.id.answer_a);
answer_b = (EditText) findViewById(R.id.answer_b);
answer_c1 = (CheckBox) findViewById(R.id.answer_c1);
answer_c2 = (CheckBox) findViewById(R.id.answer_c2);
answer_c3 = (CheckBox) findViewById(R.id.answer_c3);
answer_c4 = (CheckBox) findViewById(R.id.answer_c4);
answer_d = (EditText) findViewById(R.id.answer_d);
submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
correct = 0;
if (answer_a.isChecked()){
correct++;
}
if (answer_b.getText().toString().equals("2020")){
correct++;
}
if (answer_c1.isChecked()&&answer_c2.isChecked()&&answer_c3.isChecked()&&!answer_c4.isChecked()){
correct++;
}
if (answer_d.getText().toString().toLowerCase().equals("arun jaitley")){
correct++;
}
result.setText("The Score is " + correct + " on 4!");
}
});
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context="me.thirumurugan.quiz.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/welcome_messege"
android:textAlignment="center"
android:id="@+id/result"
android:padding="@dimen/padding"
android:textStyle="bold"
android:textSize="16sp"
android:background="@color/colorAccent"
android:textColor="#FFF"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/padding"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/padding"
android:text="@string/question_1"
android:textColor="@color/colorPrimary"
android:textStyle="bold" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/answer_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/answer_1_a" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/answer_1_b" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/padding"
android:layout_marginTop="@dimen/padding"
android:text="@string/question_2"
android:textColor="@color/colorPrimary"
android:textStyle="bold" />
<EditText
android:id="@+id/answer_b"
android:hint="@string/answer_hint"
android:textColorHint="@color/colorAccent"
android:layout_width="match_parent"
android:textSize="14sp"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorPrimary"
tools:targetApi="lollipop" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/padding"
android:layout_marginTop="@dimen/padding"
android:text="@string/question_3"
android:textColor="@color/colorPrimary"
android:textStyle="bold" />
<CheckBox
android:id="@+id/answer_c1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/answer_3_a" />
<CheckBox
android:id="@+id/answer_c2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/answer_3_b" />
<CheckBox
android:id="@+id/answer_c3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/answer_3_c" />
<CheckBox
android:id="@+id/answer_c4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/answer_3_d" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/padding"
android:layout_marginTop="@dimen/padding"
android:text="@string/question_4"
android:textColor="@color/colorPrimary"
android:textStyle="bold" />
<EditText
android:id="@+id/answer_d"
android:hint="@string/answer_hint"
android:textSize="14sp"
android:textColorHint="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorPrimary"
tools:targetApi="lollipop" />
<Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorPrimary"
android:text="@string/submit"
android:textColor="#FFFFFF"
android:textStyle="bold"
tools:targetApi="lollipop" />
</LinearLayout>
</ScrollView>
</LinearLayout>
在跟蹤錯誤消息在以下:
08-10 21點58 :06.301 20947-20947/me.thirumurugan.quiz E/AndroidR取消時間: 致命例外:主進程:me.thirumurugan.quiz,PID:20947
java.lang.NullPointerException:試圖調用虛擬方法'void android.widget.TextView.setText(java.lang.CharSequence)'on null 對象引用 me.thirumurugan.quiz.MainActivity $ 1.onClick(MainActivity.java:52)at android.view.View.performClick(View.java:5637)at android.view.View $ PerformClick。運行(View.java:22429)at android.os.Handler.handleCallback(Handler.java:751)at android.os.Handler.dispatchMessage(Handler.java:95)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)
我剛剛複製你的代碼,並粘貼在我的android工作室,它的工作就好沒有錯誤 – Ali
嘗試清理和構建你的應用程序,也使用斷點來調試你的應用程序 – Ali
我同意阿里,這裏沒有什麼看起來破碎。你應該做一個乾淨的建設。 – anomeric