作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嗯,我的程序一直给我一个空点异常,我不知道为什么?我知道它与lvl变量有关,但我不知道是什么?我该如何解决这个问题?
日志猫:
03-18 16:14:55.852: ERROR/AndroidRuntime(277): FATAL EXCEPTION: main
03-18 16:14:55.852: ERROR/AndroidRuntime(277): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.games.think/com.games.think.Think}: java.lang.NullPointerException
03-18 16:14:55.852: ERROR/AndroidRuntime(277): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-18 16:14:55.852: ERROR/AndroidRuntime(277): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-18 16:14:55.852: ERROR/AndroidRuntime(277): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-18 16:14:55.852: ERROR/AndroidRuntime(277): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-18 16:14:55.852: ERROR/AndroidRuntime(277): at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 16:14:55.852: ERROR/AndroidRuntime(277): at android.os.Looper.loop(Looper.java:123)
03-18 16:14:55.852: ERROR/AndroidRuntime(277): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-18 16:14:55.852: ERROR/AndroidRuntime(277): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 16:14:55.852: ERROR/AndroidRuntime(277): at java.lang.reflect.Method.invoke(Method.java:521)
03-18 16:14:55.852: ERROR/AndroidRuntime(277): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-18 16:14:55.852: ERROR/AndroidRuntime(277): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-18 16:14:55.852: ERROR/AndroidRuntime(277): at dalvik.system.NativeStart.main(Native Method)
03-18 16:14:55.852: ERROR/AndroidRuntime(277): Caused by: java.lang.NullPointerException
03-18 16:14:55.852: ERROR/AndroidRuntime(277): at com.games.think.Think.onCreate(Think.java:38)
03-18 16:14:55.852: ERROR/AndroidRuntime(277): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-18 16:14:55.852: ERROR/AndroidRuntime(277): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-18 16:14:55.852: ERROR/AndroidRuntime(277): ... 11 more
这是我的一些代码:
package com.games.think;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
public class Think extends Activity implements OnClickListener{
int question = 1, lvl;
/** Called when the activity is first created. */
RadioButton lvl1;
RadioButton lvl2;
RadioButton lvl3;
RadioButton lvl4;
RadioButton lvl5;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button play = (Button)findViewById(R.id.play);
play.setOnClickListener(this);
Button level = (Button)findViewById(R.id.level);
level.setOnClickListener(this);
Button exit = (Button)findViewById(R.id.exit);
exit.setOnClickListener(this);
Button setLevel = (Button)findViewById(R.id.setLevel);
setLevel.setOnClickListener(this);
lvl1 = (RadioButton)findViewById(R.id.lvl1);
lvl2 = (RadioButton)findViewById(R.id.lvl2);
lvl3 = (RadioButton)findViewById(R.id.lvl3);
lvl4 = (RadioButton)findViewById(R.id.lvl4);
lvl5 = (RadioButton)findViewById(R.id.lvl5);
lvl = getLevel();
if(lvl == -1) {
lvl=getLevel();
}
}
@SuppressWarnings("null")
private int getLevel() {
String FILENAME = "think_level";
FileInputStream fis;
byte[] buffer = new byte[1000];
try {
fis = openFileInput(FILENAME);
} catch (FileNotFoundException e) {
setLevel("1");
return -1;
}
try {
fis.read(buffer,0,1000);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String level = buffer.toString();
int iLevel = Integer.valueOf(level);
return iLevel;
}
private void setLevel(String level) {
String FILENAME = "think_level";
String string = level;
FileOutputStream fos;
try {
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
switch( v.getId()){
case R.id.play:
setContentView(R.layout.play);
setQuestion();
case R.id.level:
setContentView(R.layout.level);
switch(getLevel()) {
case 1:
lvl1.setChecked(true);
case 2:
lvl2.setChecked(true);
case 3:
lvl3.setChecked(true);
case 4:
lvl4.setChecked(true);
case 5:
lvl5.setChecked(true);
}
case R.id.setLevel:
if(lvl1.isChecked()) {
setLevel("1");
}
if(lvl2.isChecked()) {
setLevel("2");
}
if(lvl3.isChecked()) {
setLevel("3");
}
if(lvl4.isChecked()) {
setLevel("4");
}
if(lvl5.isChecked()) {
setLevel("5");
}
}
}
private void setQuestion() {
}
}
最佳答案
检查 ID 为“setLevel”的按钮是否在您的 main.xml 中。如果在其他地方,你找不到它,如下所示:
Button setLevel = (Button)findViewById(R.id.setLevel);
但是你需要一个充气机。
关于java - Android程序问题,NullPointerExc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5363268/
嗯,我的程序一直给我一个空点异常,我不知道为什么?我知道它与lvl变量有关,但我不知道是什么?我该如何解决这个问题? 日志猫: 03-18 16:14:55.852: ERROR/AndroidRun
我有JavaFX表单,用户可以选择图像。图像存储在 ArrayList 中,因为我无法将其直接分配给 lambda 表达式内的 Image 字段 List imageBinary = new Arra
我是一名优秀的程序员,十分优秀!