2011-11-22 52 views
2

我正在做一個測試,以在Eclipse中獲得Android開發。按下計算按鈕時,我的應用程序崩潰。看起來問題出在AlertDialog上,但我發現的所有例子都有同樣的方法。Android應用程序:NullPointerException

package mobile.ildu.AndroidTest; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 

public class AndroidTestActivity extends Activity { 
    private EditText text1; 
    private EditText text2; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 

    public void myClickHandler(View view) 
    { 
     switch(view.getId()) 
     { 
      case R.id.calculate: 
       if(text1.getText().length()==0) 
       { 
        callDialog("Error","You must enter a number in the first box"); 
        return; 
       } 
       if(text2.getText().length()==0) 
       { 
        callDialog("Error","You must enter a number in the second box"); 
        return; 
       } 
       float number1 = Float.parseFloat(text1.getText().toString()); 
       float number2 = Float.parseFloat(text2.getText().toString()); 
       float answer = number1*number2; 
       callDialog("Answer",number1+"*"+number2+" = "+answer); 
       break; 
     } 
    } 

    private void callDialog(String title, String message) 
    { 
     AlertDialog alertDialog= new AlertDialog.Builder(AndroidTestActivity.this).create(); 
     alertDialog.setTitle(title); 
     alertDialog.setMessage(message); 
     alertDialog.setButton("Close", new DialogInterface.OnClickListener() 
     {   
      public void onClick(DialogInterface dialog, int which) 
      { 
       dialog.dismiss(); 
      } 
     }); 
     alertDialog.show(); 
    } 
} 

錯誤日誌:

11-22 10:41:45.092: E/AndroidRuntime(675): FATAL EXCEPTION: main 
11-22 10:41:45.092: E/AndroidRuntime(675): java.lang.IllegalStateException: Could not execute method of the activity 
11-22 10:41:45.092: E/AndroidRuntime(675): at android.view.View$1.onClick(View.java:3019) 
11-22 10:41:45.092: E/AndroidRuntime(675): at android.view.View.performClick(View.java:3460) 
11-22 10:41:45.092: E/AndroidRuntime(675): at android.view.View$PerformClick.run(View.java:13955) 
11-22 10:41:45.092: E/AndroidRuntime(675): at android.os.Handler.handleCallback(Handler.java:605) 
11-22 10:41:45.092: E/AndroidRuntime(675): at android.os.Handler.dispatchMessage(Handler.java:92) 
11-22 10:41:45.092: E/AndroidRuntime(675): at android.os.Looper.loop(Looper.java:137) 
11-22 10:41:45.092: E/AndroidRuntime(675): at android.app.ActivityThread.main(ActivityThread.java:4340) 
11-22 10:41:45.092: E/AndroidRuntime(675): at java.lang.reflect.Method.invokeNative(Native Method) 
11-22 10:41:45.092: E/AndroidRuntime(675): at java.lang.reflect.Method.invoke(Method.java:511) 
11-22 10:41:45.092: E/AndroidRuntime(675): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
11-22 10:41:45.092: E/AndroidRuntime(675): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
11-22 10:41:45.092: E/AndroidRuntime(675): at dalvik.system.NativeStart.main(Native Method) 
11-22 10:41:45.092: E/AndroidRuntime(675): Caused by: java.lang.reflect.InvocationTargetException 
11-22 10:41:45.092: E/AndroidRuntime(675): at java.lang.reflect.Method.invokeNative(Native Method) 
11-22 10:41:45.092: E/AndroidRuntime(675): at java.lang.reflect.Method.invoke(Method.java:511) 
11-22 10:41:45.092: E/AndroidRuntime(675): at android.view.View$1.onClick(View.java:3014) 
11-22 10:41:45.092: E/AndroidRuntime(675): ... 11 more 
11-22 10:41:45.092: E/AndroidRuntime(675): Caused by: java.lang.NullPointerException 
11-22 10:41:45.092: E/AndroidRuntime(675): at mobile.ildu.AndroidTest.AndroidTestActivity.myClickHandler(AndroidTestActivity.java:27) 
11-22 10:41:45.092: E/AndroidRuntime(675): ... 14 more 

感謝您的幫助!

text1.getText().length() 

你會得到一個空指針異常:

+0

這是第27行? –

+0

哪一行是27行? –

回答

1

試試這個:

package mobile.ildu.AndroidTest; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 

public class AndroidTestActivity extends Activity { 
    private EditText text1; 
    private EditText text2; 
    private Button button; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     text1 = (EditText)findViewById(R.id.text1); 
     text2 = (EditText)findViewById(R.id.text2); 
     button = (Button) findViewById(R.id.calculate); 

     button.setOnClickListener(new OnClickListener() { 

      public void onClick(View arg0) { 

       if(text1.getText().length()==0) 
       { 
        callDialog("Error","You must enter a number in the first box"); 
        return; 
       } 
       if(text2.getText().length()==0) 
       { 
        callDialog("Error","You must enter a number in the second box"); 
        return; 
       } 
       float number1 = Float.parseFloat(text1.getText().toString()); 
       float number2 = Float.parseFloat(text2.getText().toString()); 
       float answer = number1*number2; 
       callDialog("Answer",number1+"*"+number2+" = "+answer); 
      } 
     }); 
    } 

    private void callDialog(String title, String message) 
    { 
     AlertDialog alertDialog= new AlertDialog.Builder(AndroidTestActivity.this).create(); 
     alertDialog.setTitle(title); 
     alertDialog.setMessage(message); 
     alertDialog.setButton("Close", new DialogInterface.OnClickListener() 
     {   
      public void onClick(DialogInterface dialog, int which) 
      { 
       dialog.dismiss(); 
      } 
     }); 
     alertDialog.show(); 
    } 
} 
+0

謝謝。我的印象是這個錯誤發生在AlertDialog部分。忽略簡單的東西.. – kieblera5

2

,當你這樣做你從未初始化的EditText成員變量等等。你叫setContentView在此之後添加到您onCreate方法:

text1 = (EditText)findViewById(R.id.idOfWidget); 
text2 = (EditText)findViewById(R.id.idOfWidget2); 

(用正確的常數當然替換那些2個IDS)。

0

除非您將它們設置在其他地方,而您沒有在這裏顯示,否則text1text2不包含任何引用,因此當您嘗試對它們調用方法時,它只會崩潰。

0

EditText,你應該使用讓他們:text1 = findViewById(R.id.txt1);

0

你還沒有向我們展示瞭如何用不完/調用myClickHandler()。但是,無論如何,這不是處理點擊的正確方法。要處理點擊,您需要通過其ID搜索按鈕,如Button button = (Button)findViewById(1);,然後致電button.setOnClickListener(this)(假設您實施了View.OnClickListener,否則傳入匿名偵聽器)。

您能否解釋myClickHandler如何/何時被調用?發佈完整的代碼..

+0

myClickHandler是從xml文件中的calculate按鈕的onClick中調用的。 – kieblera5

0
text1 = (EditText)findViewById(R.id.text1); 
    text2 = (EditText)findViewById(R.id.text2); 
相關問題