我收到空指针错误 我怎么能解决这个问题?
我该如何解决这个问题? 请帮忙 感谢
simple_ques.java
package com.mimoh.kulkarni.mimoh;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class simple_ques extends AppCompatActivity implements View.OnClickListener{
private allstrings ostring = new allstrings();
TextView qcurr,qtotal,scoreno,question;
Button bchoice1,bchoice2,bchoice3,bchoice4,bback,bans,bforward;
private String mans;
int qno=0,score=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_ques);
qcurr = (TextView)findViewById(R.id.curr);
qtotal = (TextView)findViewById(R.id.total);
scoreno = (TextView)findViewById(R.id.score);
question = (TextView)findViewById(R.id.question);
bchoice1 = (Button)findViewById(R.id.bchoice1);
bchoice2 = (Button)findViewById(R.id.bchoice2);
bchoice3 = (Button)findViewById(R.id.bchoice3);
bchoice4 = (Button)findViewById(R.id.bchoice4);
bback = (Button)findViewById(R.id.bback);
bans = (Button)findViewById(R.id.banswer);
bforward = (Button)findViewById(R.id.bforward);
bback.setOnClickListener(this);
bans.setOnClickListener(this);
bforward.setOnClickListener(this);
bchoice1.setOnClickListener(this);
bchoice2.setOnClickListener(this);
bchoice3.setOnClickListener(this);
bchoice4.setOnClickListener(this);
updatequestion();
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.bchoice1:
if(bchoice1.getText() == mans){
score = score + 1;
scoreno.setText(score);
updatequestion();
Toast.makeText(this,"Correct",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(this,"Wrong",Toast.LENGTH_LONG).show();
}
break;
case R.id.bchoice2:
if(bchoice2.getText() == mans){
score = score + 1;
scoreno.setText(score);
updatequestion();
Toast.makeText(this,"Correct",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(this,"Wrong",Toast.LENGTH_LONG).show();
}
break;
case R.id.bchoice3:
if(bchoice3.getText() == mans){
score = score + 1;
scoreno.setText(score);
updatequestion();
Toast.makeText(this,"Correct",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(this,"Wrong",Toast.LENGTH_LONG).show();
}
break;
case R.id.bchoice4:
if(bchoice4.getText() == mans){
score = score + 1;
scoreno.setText(score);
updatequestion();
Toast.makeText(this,"Correct",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(this,"Wrong",Toast.LENGTH_LONG).show();
}
break;
}
}
private void updatequestion(){
question.setText(ostring.getquestion(qno));
bchoice1.setText(ostring.getchoice1(qno));
bchoice2.setText(ostring.getchoice2(qno));
bchoice3.setText(ostring.getchoice3(qno));
bchoice4.setText(ostring.getchoice4(qno));
mans = ostring.get_corr_answer(qno);
qno++;
}
}
allstrings.java
package com.mimoh.kulkarni.mimoh;
public class allstrings {
private String simple_questions[] ={
"What is your name ?",
"What is your age ?",
"What is your surname ?"
};
private String simple_answers[][] = {
{"Mimoh","Kulkarni","Mayur","Mangesh"},
{"root","trunk","stem","leaves"},
{"root","trunk","stem","leaves"}
};
private String corr_simple_answers[] ={"Mimoh","stem","leaves"};
public String getquestion(int a){
String question = simple_questions[a];
return question;
}
public String getchoice1(int a){
String choice = simple_answers[a][0];
return choice;
}
public String getchoice2(int a){
String choice = simple_answers[a][1];
return choice;
}
public String getchoice3(int a){
String choice = simple_answers[a][2];
return choice;
}
public String getchoice4(int a){
String choice = simple_answers[a][3];
return choice;
}
public String get_corr_answer(int a){
String answer = corr_simple_answers[a];
return answer;
}
}
logcat错误: -
02-09 22:55:45.719 5026-5026/com.mimoh.kulkarni.mimoh D/AndroidRuntime: Shutting down VM
02-09 22:55:45.719 5026-5026/com.mimoh.kulkarni.mimoh E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mimoh.kulkarni.mimoh, PID: 5026
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mimoh.kulkarni.mimoh/com.mimoh.kulkarni.mimoh.simple_ques}: 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:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
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.mimoh.kulkarni.mimoh.simple_ques.updatequestion(simple_ques.java:122)
at com.mimoh.kulkarni.mimoh.simple_ques.onCreate(simple_ques.java:50)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
02-09 22:55:45.730 1732-1744/system_process W/ActivityManager: Force finishing activity com.mimoh.kulkarni.mimoh/.simple_ques
02-09 22:55:45.829 1732-1744/system_process W/ActivityManager: Force finishing activity com.mimoh.kulkarni.mimoh/.homepage
simple_ques.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:orientation="horizontal"
android:background="@drawable/topbottom">
<TextView
android:id="@+id/curr"
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="@string/currq"
android:textAlignment="viewEnd"
android:textSize="25sp"/>
<TextView
android:id="@+id/total"
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="@string/totalq"
android:textAlignment="viewStart"
android:textSize="25sp"/>
<TextView
android:id="@+id/namescore"
android:layout_width="120dp"
android:layout_height="match_parent"
android:text="@string/score"
android:textAlignment="viewEnd"
android:textSize="25sp"
android:textColor="#ff0000"/>
<TextView
android:id="@+id/score"
android:layout_width="80dp"
android:layout_height="match_parent"
android:text="@string/score"
android:textSize="25sp"
android:textAlignment="viewStart"
android:textColor="#ff0000"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="80"
android:weightSum="10"
android:background="@drawable/middle">
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:text="@string/question"
android:textSize="30sp"
android:padding="5dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
android:orientation="vertical">
<Button
android:id="@+id/bchoice1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button" />
<android.support.v4.widget.Space
android:layout_width="match_parent"
android:layout_height="10dp" />
<Button
android:id="@+id/bchoice2"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button" />
<android.support.v4.widget.Space
android:layout_width="match_parent"
android:layout_height="10dp" />
<Button
android:id="@+id/bchoice3"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button" />
<android.support.v4.widget.Space
android:layout_width="match_parent"
android:layout_height="10dp" />
<Button
android:id="@+id/bchoice4"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="12"
android:weightSum="5"
android:background="@drawable/topbottom">
<android.support.v4.widget.Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="@+id/bback"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/back"/>
<Button
android:id="@+id/banswer"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/ans_button"/>
<Button
android:id="@+id/bforward"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/forward"/>
</LinearLayout>
我收到空指针错误 我尝试了太多方法来解决这个错误。 我怎么能解决这个问题? 请帮忙 感谢
这只是为了在帖子中添加扣留请忽略
答案 0 :(得分:1)
似乎updatequestion
上使用的变量之一由null
初始化。怎么样?
我认为其中一个资源ID不正确。检查资源文件(布局文件)(。xml)。
要进行调试,您可以
Log.i("QUESTION", (question == null) ? "NULL" : "NOT NULL")
Log.i("BCHOICE1", (bchoice1 == null) ? "NULL" : "NOT NULL")
Log.i("BCHOICE2", (bchoice2 == null) ? "NULL" : "NOT NULL")
Log.i("BCHOICE3", (bchoice3 == null) ? "NULL" : "NOT NULL")
Log.i("BCHOICE4", (bchoice4 == null) ? "NULL" : "NOT NULL")
这可以放在updatequestion
的开头,而 "NULL"
将指示什么获取调用方法的空指针。