我正在尝试从带有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):在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):at java.lang.reflect.Method.invokeNative(Native Method) 07-30 00:59:00.617:E / AndroidRuntime(239):at java.lang.reflect.Method.invoke(Method.java:521) 07-30 00:59:00.617:E / AndroidRuntime(239):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:860) 07-30 00:59:00.617:E / AndroidRuntime(239):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 07-30 00:59:00.617:E / AndroidRuntime(239):at 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):at 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更多
任何人都可以帮我解决这个问题吗?
答案 0 :(得分:1)
您的调查变量为空,因为您没有初始化它。你这样做:
SurveyAdapter survey;
并保持原样。
这就是你得到NullPointerException错误的原因。