这是所有错误消息:
java.lang.RuntimeException: Unable to start activity ComponentInfo{johnny.newopen/johnny.newopen.easy}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null object reference
at johnny.newopen.easy.onCreate(easy.java:126)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
以下是相关代码:
public class easy extends AppCompatActivity
{
Button solve;
private Chronometer chronometer;
TextView plus1, plus2, minus1, minus2, multy1, multy2, points;
int p1, p2, m1, m2, mu1, mu2, p, p_ans, m_ans, mu_ans;
long time;
private DBHelper mydb;
boolean check, check1, check2, check3, check4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_easy);
mydb = new DBHelper(this);
if (check1 && check2 && check3) {
p = p + 60 - (int) time;
}
mydb.insertPoints(p);
points = (TextView)findViewById(R.id.points);
int x;
try {
{x = mydb.getPoints();}
}
catch (Exception e) {x=0;}
points.setText(x);
}
}
我不断得到指向点的空指针。我在做什么错了?
答案 0 :(得分:1)
好像在设置文本时TextView为null。查看您的代码(尚未完成),您的整数x尚未初始化。设置x = 0后尝试运行应用程序,还尝试检查textView的ID。
另外,您无法为textview设置整数值,请使用String.valueOf()
像这样:points.setText(String.valueOf(x))
很抱歉,答案格式不正确;我待会儿解决。
答案 1 :(得分:0)
检查布局中TextView
的ID是否与活动“(点)”中提到的ID相同。同样,您不能直接使用TextView
将String.valueOf()
设置为整数值。
points.setText(String.valueOf(x))
答案 2 :(得分:0)
您可以使用x.toString()设置“点”的文本值。但是您应该首先检查它是否存在,因为我认为您输入的TextView ID错误。
答案 3 :(得分:0)
嘿,您不能直接将int设置为textview
简单地做
points.setText(""+x);
它将避免使用空指针。
或者如Anuj Kumar所说的那样
**points.setText(String.valueOf(x))**