2016-05-30 13 views
-3

私のAndroidアプリケーションでは、このアプリケーションを起動する際にチェックボックス、ラジオボタン、スピナーを使用しましたが、致命的なエラーが表示されます。以下のように 私のJavaコード:lauching applicationで致命的なエラーが発生しました

package com.example.exam; 

import android.os.Bundle; 
import android.app.Activity; 
import android.app.AlertDialog.Builder; 
import android.content.Context; 
import android.content.Intent; 
import android.database.sqlite.SQLiteDatabase; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.DatePicker; 
import android.widget.DatePicker.OnDateChangedListener; 
import android.widget.EditText; 
import android.widget.RadioButton; 
import android.widget.Spinner; 
import android.widget.TextView; 

public class MainActivity extends Activity implements OnItemSelectedListener, OnClickListener { 

    private TextView t_fname; 
    private EditText e_fname; 
    private EditText e_lname; 
    private TextView t_lname; 
    private TextView t_email; 
    private EditText e_email; 
    private TextView t_pw; 
    private EditText e_pw; 
    private EditText e_dob; 
    private TextView t_dob; 
    private TextView t_gender; 
    private RadioButton r_male; 
    private TextView t_hobbies; 
    private RadioButton r_female; 
    private CheckBox ch1; 
    private CheckBox ch3; 
    private CheckBox ch2; 
    private CheckBox ch4; 
    private CheckBox ch5; 
    private CheckBox ch6; 
    private TextView t_country; 
    private EditText e_country; 
    private DatePicker datePicker1; 
    private Button b1; 

    String[] place=new String[]{"India","australia","france","japan","USA","UK"}; 
    private Spinner spin; 

    SQLiteDatabase db; 
    private String gender=""; 
    private String hobbies=""; 
    private String country=""; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     //database 
     db=openOrCreateDatabase("Registration", Context.MODE_PRIVATE, null); 
     db.execSQL("CREATE TABLE IF NOT EXISTS Student(firstname VARCHAR,lastname VARCHAR," + 
       "email VARCHAR,password VARCHAR,dob VARCHAR,gender VARCHAR,hobbies VARCHAR,country VARCHAR);"); 

     //notation 
     t_fname=(TextView)findViewById(R.id.t_fname); 
     e_fname=(EditText)findViewById(R.id.e_fname); 
     t_lname=(TextView)findViewById(R.id.t_lname); 
     e_lname=(EditText)findViewById(R.id.e_lname); 
     t_email=(TextView)findViewById(R.id.t_email); 
     e_email=(EditText)findViewById(R.id.e_email); 
     t_pw=(TextView)findViewById(R.id.t_pw); 
     e_pw=(EditText)findViewById(R.id.e_pw); 
     t_dob=(TextView)findViewById(R.id.t_dob); 
     e_dob=(EditText)findViewById(R.id.e_dob); 
     t_gender=(TextView)findViewById(R.id.t_gender); 
     r_male=(RadioButton)findViewById(R.id.r_male); 
     r_female=(RadioButton)findViewById(R.id.r_female); 
     t_hobbies=(TextView)findViewById(R.id.t_hobbies); 
     ch1=(CheckBox)findViewById(R.id.ch1); 
     ch2=(CheckBox)findViewById(R.id.ch2); 
     ch3=(CheckBox)findViewById(R.id.ch3); 
     ch4=(CheckBox)findViewById(R.id.ch4); 
     ch5=(CheckBox)findViewById(R.id.ch5); 
     ch6=(CheckBox)findViewById(R.id.ch6); 
     t_country=(TextView)findViewById(R.id.t_country); 

     b1 = (Button)findViewById(R.id.b1); 
     datePicker1 = (DatePicker) findViewById(R.id.datePicker1); 
     b1.setOnClickListener(this); 

     //spinner portion// 
       Spinner spin=(Spinner)findViewById(R.id.spin); 
       spin.setOnItemSelectedListener(this); 

       ArrayAdapter aa=new ArrayAdapter(this,android.R.layout.simple_spinner_item,place); 
       aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

       spin.setAdapter(aa); 

      //date picker 
       class MyOnDateChangedListener implements OnDateChangedListener{ 

        @Override 
        public void onDateChanged(DatePicker view, int year, int monthOfYear, 
          int dayOfMonth) { 
         // TODO Auto-generated method stub 
         e_dob.setText("" + dayOfMonth + "-" + (monthOfYear + 1) + "-" + year); 
        } 

       }; 




    } 


    //checkbox portion// 

    private void CheckBoxClicked(View view) { 
     // TODO Auto-generated method stub 

     // Check that the button is now checked? 
     boolean checked = ((CheckBox) view).isChecked(); 

     // Check which radio button was clicked 
     switch(view.getId()) { 
      case R.id.ch1: 
       if (checked) 
        hobbies = "Music"; 
       break; 
      case R.id.ch2: 
       if (checked) 
        hobbies = "Travel"; 
       break; 
      case R.id.ch3: 
       if (checked) 
        hobbies = "Dance"; 
       break; 
      case R.id.ch4: 
       if (checked) 
        hobbies = "Cooking"; 
       break; 

      case R.id.ch5: 
       if (checked) 
        hobbies = "Reading"; 
       break; 
      case R.id.ch6: 
       if (checked) 
        hobbies = "Surfing"; 
       break; 

     } 
     db.execSQL("INSER INTO Registrartion VALUES('"+hobbies+"');"); 
    } 

//radio button 
    public void RadioButtonClicked(View view) { 

     //This variable will store whether the user was male or female 

     // Check that the button is now checked? 
     boolean checked = ((RadioButton) view).isChecked(); 

     // Check which radio button was clicked 
     switch(view.getId()) { 
      case R.id.r_female: 
       if (checked) 
        gender = "female"; 
       break; 
      case R.id.r_male: 
       if (checked) 
        gender = "male"; 
       break; 
     } 
     db.execSQL("INSERT INTO Registrartion VALUES('"+gender+"');"); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public void onItemSelected(AdapterView<?> arg0, View arg1, int Position, 
      long id) { 
     // TODO Auto-generated method stub 

     country = spin.getSelectedItem().toString(); 

     db.execSQL("INSERT INTO Registrartion VALUES('"+country+"');"); 
    } 

    @Override 
    public void onNothingSelected(AdapterView<?> arg0) { 
     // TODO Auto-generated method stub 
     showMessage("empty tag","plz fill the detail"); 
    } 

    private void showMessage(String title, String message) { 
     // TODO Auto-generated method stub 
     Builder builder=new Builder(this); 
     builder.setCancelable(true); 
     builder.setTitle(title); 
     builder.setMessage(message); 
     builder.show(); 
    } 


    @Override 
    public void onClick(View view) { 
     // TODO Auto-generated method stub 

     if(view==b1){ 
      if(e_fname.getText().toString().trim().length()==0 || 
       e_lname.getText().toString().trim().length()==0|| 
       e_email.getText().toString().trim().length()==0 || 
       e_pw.getText().toString().trim().length()==0|| 
       e_dob.getText().toString().trim().length()==0 || 
       gender == null|| hobbies ==null || country ==null) 
      { 
       showMessage("Error","Please enter all the fields."); 
       return; 
       } 
      db.execSQL("INSERT INTO Student VALUES('"+e_fname.getText()+"','"+e_lname.getText()+"','"+e_email.getText()+"','"+e_pw.getText()+"','"+e_dob.getText()+"','"+gender+"','"+hobbies+"','"+country+"');"); 
      showMessage("Success", "Successfully registered"); 
      clearText(); 
      Intent i=new Intent(getApplicationContext(),Next.class); 
      startActivity(i); 
     } 

    } 

    private void clearText() { 
     // TODO Auto-generated method stub 
     e_fname.setText(""); 
     e_lname.setText(""); 
     e_email.setText(""); 
     e_pw.setText(""); 
     e_dob.setText(""); 
     e_fname.requestFocus(); 
    } 



    } 

このアプリケーションを起動中に、それが停止しています。

+2

エラーは何ですか?コード全体を投稿しないでください。 –

+0

致命的な例外........ヌルポインタ例外 – elenaVamp

+2

次にこれを確認してくださいhttp://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it –

答えて

0

これはnullポインタ例外の原因ではありませんが(スタックトレースが役に立ちます)、投稿したコードには他にもいくつかのエラーがあります。あなたのonItemSelectedで

db=openOrCreateDatabase("Registration", Context.MODE_PRIVATE, null); 
db.execSQL("CREATE TABLE IF NOT EXISTS Student(firstname VARCHAR,lastname VARCHAR," + 
    "email VARCHAR,password VARCHAR,dob VARCHAR,gender VARCHAR,hobbies VARCHAR,country VARCHAR);"); 

()あなたは、次のステートメント持って呼び出します:

db.execSQL("INSERT INTO Registrartion VALUES('"+country+"');"); 

と同様の声明他の場所で、コード全体をのonCreateでは、次のコードを持っています。あなたのコードはonClick()の有効なINSERT文を含んでいるので、これらの壊れたINSERTSは何らかの目的のためのプレースホルダであると仮定しますが、これらの行のいずれかを打つと、テーブルが見つからないか、インサート。

残りの部分については、レイアウトファイルがないと私はチェックできません(データベースのオープンとテーブルの作成は問題の原因ではありません)。

関連する問題