Android 空指针异常
运行应用程序时我收到这些错误
10-09 10:20:57.138: 错误/AndroidRuntime(282): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.rika/com.rika.LatihanActivity}: java.lang.NullPointerException
10-09 10:20:57.138: 错误/AndroidRuntime(282): 原因: java.lang.NullPointerException
请任何人帮助我解决这个问题。
它的活动类
public class LatihanActivity extends Activity{
private RadioButton radioButton;
private TextView quizQuestion;
private int rowIndex = 1;
private int questNo=0;
private boolean checked=false;
private boolean flag=true;
private RadioGroup radioGroup;
String[] corrAns = new String[5];
final DatabaseHelper db = new DatabaseHelper(this);
Cursor c1;
Cursor c2;
Cursor c3;
int counter=1;
String label;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String options[] = new String[19];
final RadioGroup radiogroup = (RadioGroup) findViewById(R.id.rdbGp1);
// layout params to use when adding each radio button
LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
try {
db.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
quizQuestion = (TextView) findViewById(R.id.TextView01);
displayQuestion();
/*Displays the next options and sets listener on next button*/
Button btnNext = (Button) findViewById(R.id.btnNext);
btnNext.setOnClickListener(btnNext_Listener);
/*Saves the selected values in the database on the save button*/
Button btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(btnSave_Listener);
c3 = db.getCorrAns();
for (int i=0;i<=4;i++) {
corrAns[i]=c3.getString(0);
c3.moveToNext();
}
radioGroup = (RadioGroup) findViewById(R.id.rdbGp1);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
for(int i=0; i<radiogroup.getChildCount() ; i++) {
RadioButton btn = (RadioButton) radioGroup.getChildAt(i);
String text;
if (btn.isPressed() && btn.isChecked() && questNo < 5) {
Log.e("corrAns[questNo]",corrAns[questNo]);
if (corrAns[questNo].equals(btn.getText()) && flag==true) {
flag=false;
checked = true;
} else if(checked==true) {
flag=true;
checked = false;
}
}
}
}
});
}
/*Called when next button is clicked*/
private View.OnClickListener btnNext_Listener= new View.OnClickListener() {
@Override
public void onClick(View v) {
flag=true;
checked = false;
questNo++;
if (questNo < 5) {
c1.moveToNext();
displayQuestion();
}
}
};
private View.OnClickListener btnSave_Listener= new View.OnClickListener() {
@Override
public void onClick(View v) {}
};
private void displayQuestion() {
//Fetching data quiz data and incrementing on each click
c1=db.getQuiz_Content(rowIndex);
c2 =db.getAns(rowIndex++);
quizQuestion.setText(c1.getString(0));
radioGroup.removeAllViews();
for (int i=0;i<=3;i++) {
//Generating and adding 4 radio buttons dynamically
radioButton = new RadioButton(this);
radioButton.setText(c2.getString(0));
radioButton.setId(i);
c2.moveToNext();
radioGroup.addView(radioButton);
}
}
}
运行应用程序时发生所有这些问题
10-09 11:13:15.128:错误/HierarchicalStateMachine(68):TetherMaster - 未处理的消息:msg.what=3
10-09 11:13:56.556: 错误/AndroidRuntime(282): 致命异常:main
10-09 11:13:56.556: 错误/AndroidRuntime(282): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.rika/com.rika.LatihanActivity}: java.lang.NullPointerException
10-09 11:13:56.556: 错误/AndroidRuntime(282): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-09 11:13:56.556: 错误/AndroidRuntime(282): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-09 11:13:56.556: 错误/AndroidRuntime(282): 在 android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-09 11:13:56.556: 错误/AndroidRuntime(282): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-09 11:13:56.556: 错误/AndroidRuntime(282): 在 android.os.Handler.dispatchMessage(Handler.java:99)
10-09 11:13:56.556: 错误/AndroidRuntime(282): 在 android.os.Looper.loop(Looper.java:123)
10-09 11:13:56.556: 错误/AndroidRuntime(282): 在 android.app.ActivityThread.main(ActivityThread.java:4627)
10-09 11:13:56.556: 错误/AndroidRuntime(282): 在 java.lang.reflect.Method.invokeNative(本机方法)
10-09 11:13:56.556: 错误/AndroidRuntime(282): 在 java.lang.reflect.Method.invoke(Method.java:521)
10-09 11:13:56.556: 错误/AndroidRuntime(282): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-09 11:13:56.556: 错误/AndroidRuntime(282): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-09 11:13:56.556: 错误/AndroidRuntime(282): 在dalvik.system.NativeStart.main(本机方法)
10-09 11:13:56.556: 错误/AndroidRuntime(282): 原因: java.lang.NullPointerException
10-09 11:13:56.556: 错误/AndroidRuntime(282): 在 com.rika.LatihanActivity.displayQuestion(LatihanActivity.java:198)
10-09 11:13:56.556: 错误/AndroidRuntime(282): 在 com.rika.LatihanActivity.onCreate(LatihanActivity.java:69)
10-09 11:13:56.556: 错误/AndroidRuntime(282): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-09 11:13:56.556: 错误/AndroidRuntime(282): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-09 11:13:56.556: 错误/AndroidRuntime(282): ... 11 更多
While running the application I'm getting these errors
10-09 10:20:57.138: ERROR/AndroidRuntime(282): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rika/com.rika.LatihanActivity}: java.lang.NullPointerException
10-09 10:20:57.138: ERROR/AndroidRuntime(282): Caused by: java.lang.NullPointerException
Please anyone help me to solve this problem.
Its the activity class
public class LatihanActivity extends Activity{
private RadioButton radioButton;
private TextView quizQuestion;
private int rowIndex = 1;
private int questNo=0;
private boolean checked=false;
private boolean flag=true;
private RadioGroup radioGroup;
String[] corrAns = new String[5];
final DatabaseHelper db = new DatabaseHelper(this);
Cursor c1;
Cursor c2;
Cursor c3;
int counter=1;
String label;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String options[] = new String[19];
final RadioGroup radiogroup = (RadioGroup) findViewById(R.id.rdbGp1);
// layout params to use when adding each radio button
LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
try {
db.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
quizQuestion = (TextView) findViewById(R.id.TextView01);
displayQuestion();
/*Displays the next options and sets listener on next button*/
Button btnNext = (Button) findViewById(R.id.btnNext);
btnNext.setOnClickListener(btnNext_Listener);
/*Saves the selected values in the database on the save button*/
Button btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(btnSave_Listener);
c3 = db.getCorrAns();
for (int i=0;i<=4;i++) {
corrAns[i]=c3.getString(0);
c3.moveToNext();
}
radioGroup = (RadioGroup) findViewById(R.id.rdbGp1);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
for(int i=0; i<radiogroup.getChildCount() ; i++) {
RadioButton btn = (RadioButton) radioGroup.getChildAt(i);
String text;
if (btn.isPressed() && btn.isChecked() && questNo < 5) {
Log.e("corrAns[questNo]",corrAns[questNo]);
if (corrAns[questNo].equals(btn.getText()) && flag==true) {
flag=false;
checked = true;
} else if(checked==true) {
flag=true;
checked = false;
}
}
}
}
});
}
/*Called when next button is clicked*/
private View.OnClickListener btnNext_Listener= new View.OnClickListener() {
@Override
public void onClick(View v) {
flag=true;
checked = false;
questNo++;
if (questNo < 5) {
c1.moveToNext();
displayQuestion();
}
}
};
private View.OnClickListener btnSave_Listener= new View.OnClickListener() {
@Override
public void onClick(View v) {}
};
private void displayQuestion() {
//Fetching data quiz data and incrementing on each click
c1=db.getQuiz_Content(rowIndex);
c2 =db.getAns(rowIndex++);
quizQuestion.setText(c1.getString(0));
radioGroup.removeAllViews();
for (int i=0;i<=3;i++) {
//Generating and adding 4 radio buttons dynamically
radioButton = new RadioButton(this);
radioButton.setText(c2.getString(0));
radioButton.setId(i);
c2.moveToNext();
radioGroup.addView(radioButton);
}
}
}
All these issues happened when run the application
10-09 11:13:15.128: ERROR/HierarchicalStateMachine(68): TetherMaster - unhandledMessage: msg.what=3
10-09 11:13:56.556: ERROR/AndroidRuntime(282): FATAL EXCEPTION: main
10-09 11:13:56.556: ERROR/AndroidRuntime(282): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rika/com.rika.LatihanActivity}: java.lang.NullPointerException
10-09 11:13:56.556: ERROR/AndroidRuntime(282): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-09 11:13:56.556: ERROR/AndroidRuntime(282): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-09 11:13:56.556: ERROR/AndroidRuntime(282): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-09 11:13:56.556: ERROR/AndroidRuntime(282): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-09 11:13:56.556: ERROR/AndroidRuntime(282): at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 11:13:56.556: ERROR/AndroidRuntime(282): at android.os.Looper.loop(Looper.java:123)
10-09 11:13:56.556: ERROR/AndroidRuntime(282): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-09 11:13:56.556: ERROR/AndroidRuntime(282): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 11:13:56.556: ERROR/AndroidRuntime(282): at java.lang.reflect.Method.invoke(Method.java:521)
10-09 11:13:56.556: ERROR/AndroidRuntime(282): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-09 11:13:56.556: ERROR/AndroidRuntime(282): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-09 11:13:56.556: ERROR/AndroidRuntime(282): at dalvik.system.NativeStart.main(Native Method)
10-09 11:13:56.556: ERROR/AndroidRuntime(282): Caused by: java.lang.NullPointerException
10-09 11:13:56.556: ERROR/AndroidRuntime(282): at com.rika.LatihanActivity.displayQuestion(LatihanActivity.java:198)
10-09 11:13:56.556: ERROR/AndroidRuntime(282): at com.rika.LatihanActivity.onCreate(LatihanActivity.java:69)
10-09 11:13:56.556: ERROR/AndroidRuntime(282): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-09 11:13:56.556: ERROR/AndroidRuntime(282): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-09 11:13:56.556: ERROR/AndroidRuntime(282): ... 11 more
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
情况1:你应该在清单文件中声明你的Activity,
如果你已经这样做了但它仍然不起作用,如果你的活动位于另一个包中,你应该用包的名称来声明它:
最后,清理并重新执行您的项目。
可能性 2:
确保您的 RadioButtonGroup 已在 main.xml 布局中定义,如果是,请确保您没有导入
android.R
;您应该使用 importYourPackage.R;
并最后清理并重建您的项目。
注意:您没有提供 NullPointerException 的完整堆栈跟踪,因此我们无法确定代码中的确切问题是什么,
Possibility 1 : you should declare your Acttivity on your Manifest File ,
if you already did it and it still doesn't work , you should declare it with the name of the package if you activity is in another package :
and at last , Clean and RE execute your project .
Possibility 2 :
make sure that your RadioButtonGroup is defined on your main.xml layout , if it is , soo make sure that you didn't import the
android.R
; you should use importYourPackage.R;
and at last Clean and rebuild your project .
NB : you didn't give the full stack trace of your NullPointerException , so we can't determine what is the exact problem in your code ,
我认为您正在创建数据库,但您应该在创建数据库后打开它。
好吧,我只是猜测。如果你能展示你的数据库类那么它将更有帮助。
或者您可以访问 http://androidlearningbegin.blogspot .com/2011/10/android-application-to-connect-to-your.html(如果您使用的是外部创建的 sqlite 数据库)。
I think you are creating database but u should open it after creating the database.
Well i m just guessing. if you will show your database class then it will be more helpful.
Or you can visit http://androidlearningbegin.blogspot.com/2011/10/android-application-to-connect-to-your.html if you are using externally created sqlite database.