lauching应用程序android中的致命错误

时间:2016-05-30 14:02:53

标签: android nullpointerexception

在我的Android应用程序中,我也使用了checkbox,单选按钮和微调器。但在启动此应用程序时,它显示致命错误。 我的java代码如下:

 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();
    }



    }

启动此应用程序时,它已停止。

1 个答案:

答案 0 :(得分: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+"');");

以及代码中其他地方的类似陈述。我认为这些破坏的INSERTS是出于某种目的的占位符,因为你的代码在onClick()中包含一个有效的INSERT语句,但点击任何这些行将导致你的代码因缺少表而失败,或者在一种情况下因为你说INSER而不是INSERT。

至于其余部分,如果没有你的布局文件,我可以检查其他一些内容(但是你的数据库打开和表创建不会导致问题)。