我正在尝试从带有 android 的应用程序中的表单中获取答案。
这是我使用的代码:
package com.stage.sondage;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class Questionnaire extends Activity {
EditText zone ;
RadioGroup mRadioGroup1;
RadioGroup mRadioGroup2;
RadioGroup mRadioGroup3;
RadioGroup mRadioGroup4;
RadioGroup mRadioGroup5;
RadioButton b1;
RadioButton b2;
RadioButton b3;
RadioButton b4;
RadioButton b5;
Button enregistrer;
//Création d'une instance de ma classe LivresBDD
SurveyAdapter survey;
Appreciation appreciation;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.questions);
// We get a reference on the radio-group widget
mRadioGroup1 = ( RadioGroup )findViewById( R.appreciation.rep1 );
mRadioGroup2 = ( RadioGroup )findViewById( R.appreciation.rep2 );
mRadioGroup3 = ( RadioGroup )findViewById( R.appreciation.rep3 );
mRadioGroup4 = ( RadioGroup )findViewById( R.appreciation.rep4 );
mRadioGroup5 = ( RadioGroup )findViewById( R.appreciation.rep5 );
b1 = (RadioButton)findViewById(mRadioGroup1.getCheckedRadioButtonId());
appreciation.setRep1((String) b1.getText());
b2 = (RadioButton)findViewById(mRadioGroup1.getCheckedRadioButtonId());
appreciation.setRep1((String) b2.getText());
b3 = (RadioButton)findViewById(mRadioGroup1.getCheckedRadioButtonId());
appreciation.setRep1((String) b3.getText());
b4 = (RadioButton)findViewById(mRadioGroup1.getCheckedRadioButtonId());
appreciation.setRep1((String) b4.getText());
b5 = (RadioButton)findViewById(mRadioGroup1.getCheckedRadioButtonId());
appreciation.setRep1((String) b5.getText());
zone = (EditText) findViewById(R.appreciation.zone);
appreciation.setZone(zone.getText().toString());
try{
enregistrer = ( Button )findViewById( R.appreciation.enregistrer);
enregistrer.setOnClickListener( new OnClickListener() {
public void onClick( View view ) {
//On ouvre la base de données pour écrire dedans
survey.open();
//On insère le livre que l'on vient de créer
survey.insertAppreciation(appreciation);
//on ferme la base de données
survey.close();
Intent intent = new Intent(Questionnaire.this,Questionnaire.class);
startActivity(intent);
}
});
}
// If there is an exception, for example the database is not usable...
catch ( SQLiteException e ) {
Toast.makeText( this, "Impossible d'ouvrir la base de donnees", Toast.LENGTH_SHORT ).show();
}
catch ( Exception e ) {
Toast.makeText( this, "Problأ¨me... L'erreur vient probablement d'un fichier xml.", Toast.LENGTH_SHORT ).show();
}
}
}
我想从这个表格中得到答案并返回到同一个表格。
当我启动应用程序时,我从 logcat 中收到以下错误:
07-30 00:59:00.577:E/AndroidRuntime(239):未捕获的处理程序:线程主因未捕获的异常而退出 07-30 00:59:00.617:E/AndroidRuntime(239):java.lang.RuntimeException:无法开始活动 ComponentInfo{com.stage.sondage/com.stage.sondage.Questionnaire}: java.lang.NullPointerException 07-30 00:59:00.617: E/AndroidRuntime(239): at android.app.ActivityThread.performLaunchActivity(ActivityThread .java:2496) 07-30 00:59:00.617: E/AndroidRuntime(239): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 07-30 00:59:00.617: E/AndroidRuntime(239 ): 在 android.app.ActivityThread.access$2200(ActivityThread.java:119) 07-30 00:59:00.617: E/AndroidRuntime(239): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 07-30 00:59:00.617: E/AndroidRuntime(239): 在 android.os.Handler.dispatchMessage(Handler.java:99) 07-30 00:59:00.617: E/AndroidRuntime(239): 在android.os.Looper.loop(Looper.java:123) 07-30 00:59:00.617: E/AndroidRuntime(239): 在 android.app.ActivityThread.main(ActivityThread.java:4363) 07-30 00: 59:00.617: E/AndroidRuntime(239): 在 java.lang.reflect.Method.invokeNative(Native Method) 07-30 00:59:00.617: E/AndroidRuntime(239): 在 java.lang.reflect.Method。调用(Method.java:521)07-30 00:59:00.617:E/AndroidRuntime(239):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)07-30 00: 59:00.617: E/AndroidRuntime(239): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 07-30 00:59:00.617:E/AndroidRuntime(239): 在 dalvik.system.NativeStart.main(Native Method) 07-30 00:59:00.617: E/AndroidRuntime(239): 由: java.lang.NullPointerException 07-30 00:59: 00.617: E/AndroidRuntime(239): 在 com.stage.sondage.Questionnaire.onCreate(Questionnaire.java:53) 07-30 00:59:00.617: E/AndroidRuntime(239): 在 android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1047) 07-30 00:59:00.617: E/AndroidRuntime(239): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 07-30 00:59:00.617: E/AndroidRuntime (239): ... 11 更多E/AndroidRuntime(239): 在 com.stage.sondage.Questionnaire.onCreate(Questionnaire.java:53) 07-30 00:59:00.617: E/AndroidRuntime(239): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation .java:1047) 07-30 00:59:00.617: E/AndroidRuntime(239): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 07-30 00:59:00.617: E/AndroidRuntime(239 ): ... 11 更多E/AndroidRuntime(239): 在 com.stage.sondage.Questionnaire.onCreate(Questionnaire.java:53) 07-30 00:59:00.617: E/AndroidRuntime(239): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation .java:1047) 07-30 00:59:00.617: E/AndroidRuntime(239): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 07-30 00:59:00.617: E/AndroidRuntime(239 ): ... 11 更多
谁能帮我解决这个问题?