0

我有两种方法来更新和删除我的应用程序中的数据,我在我的 switch 语句中实现了尝试捕获,现在收到以下错误:

01-14 20:38:58.778: D/AndroidRuntime(273): Shutting down VM
01-14 20:38:58.778: W/dalvikvm(273): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-14 20:38:58.798: E/AndroidRuntime(273): FATAL EXCEPTION: main
01-14 20:38:58.798: E/AndroidRuntime(273): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.flybase2/com.example.flybase2.viewEdit}: java.lang.NullPointerException
01-14 20:38:58.798: E/AndroidRuntime(273):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
01-14 20:38:58.798: E/AndroidRuntime(273):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-14 20:38:58.798: E/AndroidRuntime(273):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-14 20:38:58.798: E/AndroidRuntime(273):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-14 20:38:58.798: E/AndroidRuntime(273):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 20:38:58.798: E/AndroidRuntime(273):  at android.os.Looper.loop(Looper.java:123)
01-14 20:38:58.798: E/AndroidRuntime(273):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-14 20:38:58.798: E/AndroidRuntime(273):  at java.lang.reflect.Method.invokeNative(Native Method)
01-14 20:38:58.798: E/AndroidRuntime(273):  at java.lang.reflect.Method.invoke(Method.java:521)
01-14 20:38:58.798: E/AndroidRuntime(273):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-14 20:38:58.798: E/AndroidRuntime(273):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-14 20:38:58.798: E/AndroidRuntime(273):  at dalvik.system.NativeStart.main(Native Method)
01-14 20:38:58.798: E/AndroidRuntime(273): Caused by: java.lang.NullPointerException
01-14 20:38:58.798: E/AndroidRuntime(273):  at com.example.flybase2.viewEdit.<init>(viewEdit.java:63)
01-14 20:38:58.798: E/AndroidRuntime(273):  at java.lang.Class.newInstanceImpl(Native Method)
01-14 20:38:58.798: E/AndroidRuntime(273):  at java.lang.Class.newInstance(Class.java:1429)
01-14 20:38:58.798: E/AndroidRuntime(273):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-14 20:38:58.798: E/AndroidRuntime(273):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
01-14 20:38:58.798: E/AndroidRuntime(273):  ... 11 more

任何人都可以看到这个问题吗?没有尝试捕获它可以完美地工作。

继承人类:

package com.example.flybase2;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class viewEdit extends Activity implements OnClickListener{

EditText namePassedEdit;
EditText numPassedEdit;
EditText emailPassedEdit;
EditText commentPassedEdit;
Button bUpdate;
Button bDelete;
long passedID = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.editview);

DBHandler displayEdit = new DBHandler(this, null, null);


        Bundle extras = getIntent().getExtras(); 
        if (extras != null) {
            passedID = extras.getLong("passedID"); 
        }
        displayEdit.open();
        String returnedNameToEdit = displayEdit.getName(passedID);
        String returnedNumToEdit  = displayEdit.getNum(passedID);
        String returnedEmailToEdit  = displayEdit.getEmail(passedID);
        String returnedCommentToEdit = displayEdit.getComments(passedID); 

    namePassedEdit = (EditText) findViewById(R.id.inputNameEdit);
    numPassedEdit = (EditText) findViewById(R.id.inputTelNoEdit);
    emailPassedEdit = (EditText) findViewById(R.id.inputEmailEdit);
    commentPassedEdit = (EditText) findViewById(R.id.inputCommentEdit);
    bUpdate = (Button) findViewById (R.id.btnAddConEdit);
    bDelete = (Button) findViewById (R.id.btnDeleteContact);



        namePassedEdit.setText(returnedNameToEdit);
        numPassedEdit.setText(returnedNumToEdit);
        emailPassedEdit.setText(returnedEmailToEdit);
        commentPassedEdit.setText(returnedCommentToEdit);


        bUpdate.setOnClickListener(this);
        bDelete.setOnClickListener(this);
}



String nameEdit = namePassedEdit.getText().toString();
String telEdit = numPassedEdit.getText().toString();
String emailEdit = emailPassedEdit.getText().toString();
String commentEdit = commentPassedEdit.getText().toString();

@Override
public void onClick(View updateOrDeleteClicked) {
    boolean check = true;
switch(updateOrDeleteClicked.getId()){

    case (R.id.btnAddConEdit):


        try
    {

        DBHandler updateData = new DBHandler(this, null, null);
        updateData.open();
        updateData.updateData(passedID, nameEdit, telEdit, emailEdit, commentEdit);
    } 
    catch (Exception e)
    {
        check = false;

        Dialog d = new Dialog(this);
        d.setTitle("Contact failed to be deleted.");
        TextView txt = new TextView(this);
        txt.setText("Fail");
        d.setContentView(txt);
        d.show();
    }
    finally
    {
        if(check = true);
        {

            Dialog e = new Dialog(this);
            e.setTitle("Contact deleted.");
            TextView txt = new TextView(this);
            txt.setText("Success");
            e.setContentView(txt);
            e.show();
        }
    }


        break;


    case (R.id.btnDeleteContact):





    try
{

        DBHandler deleteContact = new DBHandler(this, null, null);
        deleteContact.open();
        deleteContact.deleteData(passedID);
} 
catch (Exception e)
{
    check = false;

    Dialog d = new Dialog(this);
    d.setTitle("Contact failed to be deleted.");
    TextView txt = new TextView(this);
    txt.setText("Fail");
    d.setContentView(txt);
    d.show();
}
finally
{
    if(check = true);
    {

        Dialog e = new Dialog(this);
        e.setTitle("Contact deleted.");
        TextView txt = new TextView(this);
        txt.setText("Success");
        e.setContentView(txt);
        e.show();
    }
}


    break;
}

}
}
4

3 回答 3

2
String nameEdit = namePassedEdit.getText().toString();
String telEdit = numPassedEdit.getText().toString();
String emailEdit = emailPassedEdit.getText().toString();
String commentEdit = commentPassedEdit.getText().toString();

这些行不在方法内,您不能那样使用它们。你得到一个空指针,namePassedEdit因为它试图在创建类时初始化它们,并且还没有为它们分配任何东西。

于 2013-01-14T21:01:34.187 回答
2

我在这里看到的一个问题是:

if(check = true); //这结束了 if 语句在这里和这里你分配 true 来检查仅此而已..

我想你需要的是:

if(check){....}
于 2013-01-14T20:50:24.717 回答
1

我能找到的主要问题是您在第 59 行关闭了 onCreate 的大括号。在函数之间保留以下行。

String nameEdit = namePassedEdit.getText().toString();
String telEdit = numPassedEdit.getText().toString();
String emailEdit = emailPassedEdit.getText().toString();
String commentEdit = commentPassedEdit.getText().toString();

如果您将这些移到 onCreate 中,它可能会解决您的问题。同样正如@Nambari 在他们的回答中所说,在您的 if 语句中,您正在检查 check 是否设置为 true。您需要将其更改为:

if(check)
于 2013-01-14T21:00:30.950 回答