-1

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):at 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):atjava.lang.reflect.Method。 invoke(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(ネイティブメソッド)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詳細

誰かが私がこれを解決するのを手伝ってもらえますか?

4

1 に答える 1

1

初期化していないため、調査変数は null です。これを行うだけです:

SurveyAdapter survey;

そしてそのままにしておきます。

そのため、NullPointerException エラーが発生します。

于 2012-07-30T02:07:25.137 に答える