2012-05-20 58 views
-1

當我點擊提交Button,看看qf.xml和Math.java崩潰的地方,我的應用程序崩潰,下面的文本是從logcat(從該應用直到它崩潰)的初始化:我的Android應用程序崩潰時,使用onClick回調

 
05-20 12:08:11.671: D/libEGL(22022): loaded /system/lib/egl/libGLES_android.so 
05-20 12:08:11.686: D/libEGL(22022): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so 
05-20 12:08:11.694: D/libEGL(22022): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so 
05-20 12:08:11.694: D/libEGL(22022): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so 
05-20 12:08:11.866: D/OpenGLRenderer(22022): Enabling debug mode 0 
05-20 12:08:16.514: D/OpenGLRenderer(22022): Flushing caches (mode 0) 
05-20 12:08:22.335: D/AndroidRuntime(22022): Shutting down VM 
05-20 12:08:22.335: W/dalvikvm(22022): threadid=1: thread exiting with uncaught exception (group=0x40a421f8) 
05-20 12:08:22.342: E/AndroidRuntime(22022): FATAL EXCEPTION: main 
05-20 12:08:22.342: E/AndroidRuntime(22022): java.lang.NullPointerException 
05-20 12:08:22.342: E/AndroidRuntime(22022): at com.wael.test.Math$1$1.onClick(Math.java:85) 
05-20 12:08:22.342: E/AndroidRuntime(22022): at android.view.View.performClick(View.java:3511) 
05-20 12:08:22.342: E/AndroidRuntime(22022): at android.view.View$PerformClick.run(View.java:14105) 
05-20 12:08:22.342: E/AndroidRuntime(22022): at android.os.Handler.handleCallback(Handler.java:605) 
05-20 12:08:22.342: E/AndroidRuntime(22022): at android.os.Handler.dispatchMessage(Handler.java:92) 
05-20 12:08:22.342: E/AndroidRuntime(22022): at android.os.Looper.loop(Looper.java:137) 
05-20 12:08:22.342: E/AndroidRuntime(22022): at android.app.ActivityThread.main(ActivityThread.java:4424) 
05-20 12:08:22.342: E/AndroidRuntime(22022): at java.lang.reflect.Method.invokeNative(Native Method) 
05-20 12:08:22.342: E/AndroidRuntime(22022): at java.lang.reflect.Method.invoke(Method.java:511) 
05-20 12:08:22.342: E/AndroidRuntime(22022): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
05-20 12:08:22.342: E/AndroidRuntime(22022): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
05-20 12:08:22.342: E/AndroidRuntime(22022): at dalvik.system.NativeStart.main(Native Method) 
05-20 12:08:23.913: I/Process(22022): Sending signal. PID: 22022 SIG: 9 

這是Math.java的代碼:

package com.wael.test; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.text.InputType; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.wael.first.app.R; 

public class Math extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.math); 
     final Button button1 = (Button) findViewById(R.id.lf); 
     final Button button2 = (Button) findViewById(R.id.qf); 
     button1.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       // Perform action on click 
       setContentView(R.layout.qf); 
       final Button submit1 = (Button) findViewById(R.id.qfButton); 
       final EditText ix = (EditText) findViewById(R.id.lfET1); 
       final EditText ia = (EditText) findViewById(R.id.lfET2); 
       final EditText ib = (EditText) findViewById(R.id.lfET3); 
       final TextView result = (TextView)         findViewById(R.id.tvResultQf); 
      // setting EditText input type 
      ix.setInputType(InputType.TYPE_CLASS_NUMBER); 
      ia.setInputType(InputType.TYPE_CLASS_NUMBER); 
      ib.setInputType(InputType.TYPE_CLASS_NUMBER); 
      submit1.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        Boolean TF = true; 
        String sx = String.valueOf(ix); 
        String sa = String.valueOf(ia); 
        String sb = String.valueOf(ib); 
        if (sx.matches("")) { 
         TF = false; 
         Context context = getApplicationContext(); 
         CharSequence text = "Enter a value in x"; 
         int duration = Toast.LENGTH_SHORT; 

         Toast toast = Toast.makeText(context, text, 
           duration); 
         toast.show(); 
        } 
        if (sa.matches("")) { 
         TF = false; 
         Context context = getApplicationContext(); 
         CharSequence text = "Enter a value in a"; 
         int duration = Toast.LENGTH_SHORT; 

         Toast toast = Toast.makeText(context, text, 
           duration); 
         toast.show(); 
        } 
        if (sb.matches("")) { 
         TF = false; 
         Context context = getApplicationContext(); 
         CharSequence text = "Enter a value in b"; 
         int duration = Toast.LENGTH_SHORT; 

         Toast toast = Toast.makeText(context, text, 
           duration); 
         toast.show(); 
        } 
        if (TF == true) { 
         double x = Double.parseDouble(ix.getText() 
           .toString()); 
         double a = Double.parseDouble(ia.getText() 
           .toString()); 
         double b = Double.parseDouble(ib.getText() 
           .toString()); 
         double y = a * x + b; 
         String sy = String.valueOf(y); 
         result.setText(sy); 
        } 
       } 

      }); 
     } 
    }); 
    button2.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      // Perform action on click 
      setContentView(R.layout.qf); 
      final Button submit2 = (Button) findViewById(R.id.qfButton); 
      final EditText ix = (EditText) findViewById(R.id.qfET1); 
      final EditText ia = (EditText) findViewById(R.id.qfET2); 
      final EditText ib = (EditText) findViewById(R.id.qfET3); 
      final EditText ic = (EditText) findViewById(R.id.qfET4); 
      final TextView result = (TextView) findViewById(R.id.tvResultLf); 
      // setting EditText input type 
      ix.setInputType(InputType.TYPE_CLASS_NUMBER); 
      ia.setInputType(InputType.TYPE_CLASS_NUMBER); 
      ib.setInputType(InputType.TYPE_CLASS_NUMBER); 
      ic.setInputType(InputType.TYPE_CLASS_NUMBER); 

      submit2.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        Boolean TF = true; 
        String sx = String.valueOf(ix); 
        String sa = String.valueOf(ia); 
        String sb = String.valueOf(ib); 
        String sc = String.valueOf(ic); 
        if (sx.matches("")) { 
         TF = false; 
         Context context = getApplicationContext(); 
         CharSequence text = "Enter a value in x"; 
         int duration = Toast.LENGTH_SHORT; 

         Toast toast = Toast.makeText(context, text, 
           duration); 
         toast.show(); 
        } 
        if (sa.matches("")) { 
         TF = false; 
         Context context = getApplicationContext(); 
         CharSequence text = "Enter a value in a"; 
         int duration = Toast.LENGTH_SHORT; 

         Toast toast = Toast.makeText(context, text, 
           duration); 
         toast.show(); 
        } 
        if (sb.matches("")) { 
         TF = false; 
         Context context = getApplicationContext(); 
         CharSequence text = "Enter a value in b"; 
         int duration = Toast.LENGTH_SHORT; 

         Toast toast = Toast.makeText(context, text, 
           duration); 
         toast.show(); 
        } 
        if (sc.matches("")) { 
         TF = false; 
         Context context = getApplicationContext(); 
         CharSequence text = "Enter a value in c"; 
         int duration = Toast.LENGTH_SHORT; 

         Toast toast = Toast.makeText(context, text, 
           duration); 
         toast.show(); 
        } 
        if (TF == true) { 
         double x = Double.parseDouble(ix.getText() 
           .toString()); 
         double a = Double.parseDouble(ia.getText() 
           .toString()); 
         double b = Double.parseDouble(ib.getText() 
           .toString()); 
         double c = Double.parseDouble(ic.getText() 
           .toString()); 
         double y=a*(x*x)+b*x+c; 
         String sy = String.valueOf(y); 
         result.setText(sy); 
        } 
       } 

      }); 
     } 
    }); 
} 

}

這是qf.xml代碼(噸他佈局):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/lf" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<Space 
    android:layout_width="fill_parent" 
    android:layout_height="10dp" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/enter_all" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:textColor="#FF0000" /> 

<Space 
    android:layout_width="fill_parent" 
    android:layout_height="20dp" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/x" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<EditText 
    android:id="@+id/lfET1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:digits="true" 
    android:ems="10" 
    android:hint="@string/x" 
    android:inputType="number" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/a" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<EditText 
    android:id="@+id/lfET2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:digits="true" 
    android:ems="10" 
    android:hint="@string/b" 
    android:inputType="number" > 
</EditText> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/b" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<EditText 
    android:id="@+id/lfET3" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:digits="true" 
    android:ems="10" 
    android:hint="@string/a" 
    android:inputType="number" /> 

<Space 
    android:layout_width="fill_parent" 
    android:layout_height="20dp" /> 

<Button 
    android:id="@+id/qfButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:text="@string/sub" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/y" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<TextView 
    android:id="@+id/tvResultLf" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

</LinearLayout>  
+0

請張貼Math.java –

+0

添加的代碼提交的按鈕'onClick'法和點,這是行85 – Luksprog

+0

@Luksprog我已經加入我已經添加了佈局代碼 – wael

回答

2

一個NullPointerException意味着您試圖訪問的東西,尚未初始化。例如,如果你做這樣的事情:

Button myButton = null; 
myButton.setText("some text"); 

這是語法正確的,但myButton並不是指沒有對UI又那麼努力改變未初始化的對象的屬性,會崩潰您的應用程序。由於我看不到你的代碼,這只是一個例子,可以導致這種異常。

大約從NullPointerException

當程序嘗試訪問一個字段或一個對象的方法或一個陣列的元件當沒有實例或陣列使用拋出的文檔,即如果該對象或數組指向空。它也出現在其他一些不太明顯的情況下,如Throwable引用爲null的throw e語句。

0

編輯:OnClickListener第一個ButtonButton1的你搜索TextView(在結果)id爲tvResultQf,但你沒有一個在你R.layout.qf佈局(你有一個編號爲tvResultLf,可能是一個錯字?),當您嘗試使用它來設置文本(result.setText(sy);)時,它會拋出NullPointerException

另外在OnCLickListenerButtonBUTTON2您搜索的EditText用id qfET4但你不必在你的R.layout.qf佈局之一,所以當你嘗試設置的inputType(ic.setInputType(InputType.TYPE_CLASS_NUMBER); ),它將引發再次NullPointerException

這可能是其他錯誤,只是快速查看。


你沒有點出這是行85(拋出異常),但我猜你的TextView結果null當您嘗試設置就行了它的文字:

result.setText(sy);// the last line in your code 

如果這是行85然後檢查佈局R.layout.qf,看看是否有TextView與編號tvResultLf。如果你有它,也可以嘗試通過菜單Project - >Clean來清理項目。

如果這不起作用,請發佈R.layout.qf文件的佈局。

相關問題