2016-05-30 67 views
-3

在我的android應用程序中,我使用了checkedbox,單選按鈕和微調器,但在啓動此應用程序期間,它顯示致命錯誤。 下面我java代碼:lauching應用程序中的致命錯誤android

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

這不是空指針異常的原因(堆棧跟蹤對此很有幫助),但是您發佈的代碼中還有一些其他錯誤顯而易見。在您的onCreate有下面的代碼:

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);"); 

在你onItemSelected()調用你有下面的語句:別處整個代碼

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

和類似聲明。由於代碼在onClick()中包含有效的INSERT語句,因此我假設這些破損的INSERTS是佔位符,但因爲缺少表或在一種情況下觸發這些行中的任何一行都會導致代碼失敗,因爲您說的是INSER而不是插。

至於其餘的,沒有你的佈局文件,我可以檢查(但你的數據庫打開和表創建不會導致問題)。

相關問題