2013-01-14 41 views
0

我有兩種方法來更新和我的應用程序中刪除數據,我已經Implemeted一個我switch語句中試捕撈,我現在得到以下錯誤:嘗試捕捉 - 無法實例活動ComponentInfo

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

你在哪條線上獲得NPE? – PermGenError

+3

請不要截斷LogCat到第一行,完全發佈它。 – WarrenFaith

+1

請修正您的代碼的格式...使其難以閱讀... – t0mm13b

回答

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

這些線是不是方法裏面,你可以不喜歡使用它們。你在namePassedEdit上得到一個空指針,因爲它在創建類時試圖初始化它們,並且它們還沒有分配任何東西。

+0

是的,就像Nambari所說的那樣,它的問題在於它的第63行。我如何在一個方法中實現它們?我以爲我已經在點擊方法中指定了它們? – user1352057

+1

其實在那裏回答我自己的問題!我已經將它們插入到了該方法中,現在可以使用了,謝謝 – user1352057

2

一個問題我在這裏看到的是:

if(check = true); //此結束,如果語句在這裏和這裏你真正分配到檢查沒有什麼比這更..

我猜你所需要的就是:

if(check){....} 
+1

他只能使用'if(check)'作爲檢查是布爾值的。 – Smit

+0

@smit:你是對的。更新了答案。 – kosa

+0

@Nambari非常真實我改變了它,但這仍然不會影響我得到的錯誤。很奇怪,因爲我有相同的嘗試趕上另一個類,它工作正常。我添加了整個LogCat – user1352057

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) 
相關問題