在运行此代码时发生java.lang.nullpointerexception并强制关闭应用程序

时间:2014-07-03 11:23:45

标签: android


package com.example.minorproject;



import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.database.sqlite.SQLiteDatabase;

import android.view.Menu;

import android.view.View;

import android.widget.EditText;

import android.widget.Toast;

public class SignUp extends Activity
 {

    Intent i=null;
    boolean flag=false;
    SQLiteDatabase db=null;
    EditText tv1,tv2,tv3,tv4,tv5,tv6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);

        EditText tv1=(EditText)findViewById(R.id.name);
        EditText tv2=(EditText)findViewById(R.id.email);
        EditText tv3=(EditText)findViewById(R.id.mobile_no);
        EditText tv4=(EditText)findViewById(R.id.address);
        EditText tv5=(EditText)findViewById(R.id.blood_group);
        EditText tv6=(EditText)findViewById(R.id.password);

        db=openOrCreateDatabase("DATABASE",MODE_PRIVATE,null);
        db.execSQL("Create table if not exists BloodDonor(name varchar,email varchar,mobile_no int,address varchar,blood_group varchar,password varchar)");

    }

    public void action(View v)
    {

    switch(v.getId()){
    case R.id.login:
        Intent i=new Intent(this,Login.class);
        startActivityForResult(i, 500);
        break;

    case R.id.sign_up:
        String name=tv1.getText().toString();
        String email=tv2.getText().toString();
        String mobile=tv3.getText().toString();
        String address=tv4.getText().toString();
        String bloodgroup=tv5.getText().toString();
        String password=tv6.getText().toString();

        if(name==null||name==""||name.length()<3)
        {
            show("Please Enter Correct Name.");
        }

        else if(email==null||email==""||email.length()<10)
        {
            show("Please Enter Correct Email id.");
        }
        else if(mobile==null||mobile==""||mobile.length()<10)
        {
            show("Please Enter Correct mobile number.");
        }
        else if(address==null||address==""||address.length()<5)
        {
            show("Please Enter Correct address.");
        }
        else if(bloodgroup==null||bloodgroup==""||bloodgroup=="A+ve"||bloodgroup=="A+VE"||bloodgroup=="B+VE"
                ||bloodgroup=="B+ve"||bloodgroup=="AB+ve"||bloodgroup=="AB+VE"||bloodgroup=="O+ve"
                ||bloodgroup=="O+VE"||bloodgroup=="A-ve"||bloodgroup=="A-VE"||bloodgroup=="B-VE"
                ||bloodgroup=="B-ve"||bloodgroup=="AB-ve"||bloodgroup=="AB-VE"||bloodgroup=="O-ve"
                ||bloodgroup=="O-VE")
        {
            show("Please Enter Correct blood group.");
        }
        else if(password==null||password==""||password.length()<6)
        {
            show("Please Enter Strong Password.");
        }
        else
        {   
            db.execSQL("insert into login values('"+name+"','"+email+"','"+mobile+"','"+address+"','"+bloodgroup+"','"+password+"''nothing')");
            i=new Intent(this,Home.class);
            startActivityForResult(i, 500);

            db.close();
            finish();
        }
        break;
    }

}   


public void show(String str){

    Toast.makeText(this,str,Toast.LENGTH_LONG).show();

}   



}

这是我的logcat

07-03 07:05:55.780:D / dalvikvm(795):GC_FOR_ALLOC释放69K,7%免费2631K / 2820K,暂停129ms,总计169ms

07-03 07:05:55.819:I / dalvikvm-heap(795):将堆(frag case)增长到4.147MB以进行1536016字节分配

07-03 07:05:56.039:D / dalvikvm(795):GC_FOR_ALLOC释放2K,5%免费4128K / 4324K,暂停194ms,总计194ms

07-03 07:05:58.410:D / gralloc_goldfish(795):未检测到GPU仿真的仿真器。

07-03 07:06:12.809:D / dalvikvm(795):GC_FOR_ALLOC释放15K,4%免​​费4242K / 4376K,暂停128ms,总计130ms

07-03 07:06:12.859:I / dalvikvm-heap(795):将堆(frag case)增长到5.720MB,用于1536016字节分配

07-03 07:06:13.109:D / dalvikvm(795):GC_FOR_ALLOC释放1K,3%免费5741K / 5880K,暂停245ms,总计245ms

07-03 07:06:16.108:I / Choreographer(795):跳过75帧!应用程序可能在其主线程上做了太多工作。

07-03 07:06:22.759:I / Choreographer(795):跳过44帧!应用程序可能在其主线程上做了太多工作。

07-03 07:06:54.290:I / Choreographer(795):跳过32帧!应用程序可能在其主线程上做了太多工作。

07-03 07:07:03.780:D / AndroidRuntime(795):关闭虚拟机

07-03 07:07:03.789:W / dalvikvm(795):threadid = 1:线程退出时未捕获异常(组= 0x414c4700)

07-03 07:07:03.869:E / AndroidRuntime(795):致命异常:主

07-03 07:07:03.869:E / AndroidRuntime(795):java.lang.IllegalStateException:无法执行活动的方法

07-03 07:07:03.869:E / AndroidRuntime(795):在android.view.View $ 1.onClick(View.java:3633)

07-03 07:07:03.869:E / AndroidRuntime(795):在android.view.View.performClick(View.java:4240)

07-03 07:07:03.869:E / AndroidRuntime(795):在android.view.View $ PerformClick.run(View.java:17721)

07-03 07:07:03.869:E / AndroidRuntime(795):在android.os.Handler.handleCallback(Handler.java:730)

07-03 07:07:03.869:E / AndroidRuntime(795):在android.os.Handler.dispatchMessage(Handler.java:92)

07-03 07:07:03.869:E / AndroidRuntime(795):在android.os.Looper.loop(Looper.java:137)

07-03 07:07:03.869:E / AndroidRuntime(795):在android.app.ActivityThread.main(ActivityThread.java:5103)

07-03 07:07:03.869:E / AndroidRuntime(795):at java.lang.reflect.Method.invokeNative(Native Method)

07-03 07:07:03.869:E / AndroidRuntime(795):at java.lang.reflect.Method.invoke(Method.java:525)

07-03 07:07:03.869:E / AndroidRuntime(795):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)

07-03 07:07:03.869:E / AndroidRuntime(795):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

07-03 07:07:03.869:E / AndroidRuntime(795):at dalvik.system.NativeStart.main(Native Method)

07-03 07:07:03.869:E / AndroidRuntime(795):引起:java.lang.reflect.InvocationTargetException

07-03 07:07:03.869:E / AndroidRuntime(795):at java.lang.reflect.Method.invokeNative(Native Method)

07-03 07:07:03.869:E / AndroidRuntime(795):at java.lang.reflect.Method.invoke(Method.java:525)

07-03 07:07:03.869:E / AndroidRuntime(795):在android.view.View $ 1.onClick(View.java:3628)

07-03 07:07:03.869:E / AndroidRuntime(795):... 11更多

07-03 07:07:03.869:E / AndroidRuntime(795):引起:java.lang.NullPointerException

07-03 07:07:03.869:E / AndroidRuntime(795):at com.example.minorproject.SignUp.action(SignUp.java:47)

07-03 07:07:03.869:E / AndroidRuntime(795):... 14更多

07-03 07:07:10.749:D / dalvikvm(828):GC_FOR_ALLOC释放73K,7%免费2631K / 2824K,暂停38ms,总计41ms

07-03 07:07:10.759:I / dalvikvm-heap(828):将堆(frag case)增长到4.147MB以进行1536016字节分配

07-03 07:07:10.879:D / dalvikvm(828):GC_FOR_ALLOC释放2K,5%免费4129K / 4328K,暂停113ms,总计113ms

07-03 07:07:11.489:I / Choreographer(828):跳过30帧!应用程序可能在其主线程上做了太多工作。

07-03 07:07:11.519:D / gralloc_goldfish(828):未检测到GPU仿真的仿真器

1 个答案:

答案 0 :(得分:1)

您已将EditText对象创建为onCreate()本地,而是将该对象分配给类级对象,试试这个,

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);

        tv1=(EditText)findViewById(R.id.name);
        tv2=(EditText)findViewById(R.id.email);
        tv3=(EditText)findViewById(R.id.mobile_no);
        tv4=(EditText)findViewById(R.id.address);
        tv5=(EditText)findViewById(R.id.blood_group);
        tv6=(EditText)findViewById(R.id.password);

        db=openOrCreateDatabase("DATABASE",MODE_PRIVATE,null);
        db.execSQL("Create table if not exists BloodDonor(name varchar,email varchar,mobile_no int,address varchar,blood_group varchar,password varchar)");

    }