2012-12-18 334 views
1

我已經提出了這個應用程序,但是當我嘗試午餐時,他立即關閉。 這是我Principale.java:Android應用程序啓動時崩潰

package com.gabriele.progetti; 

import android.R.string; 
import android.os.Bundle; 
import static java.lang.Math.*; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.TextView; 

public class Principale extends Activity 
{ 
    private EditText edittext1, edittext2, edittext3; 
    private TextView textview1; 
    int A,B,C; 


protected void onCreate(Bundle savedInstanceState) // Sarebbe come il main 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_principale); 

    Calcoliamo(); 

} 
// ;) 



public void Calcoliamo() 
{ 
    edittext1 = (EditText) findViewById(R.id.editText1); 
    edittext2 = (EditText) findViewById(R.id.editText2); 
    edittext3 = (EditText) findViewById(R.id.editText3); 

    A = Integer.parseInt(edittext1.getText().toString()); 
    B = Integer.parseInt(edittext2.getText().toString()); 
    C = Integer.parseInt(edittext3.getText().toString()); 

    int Delta = (B * B)*4*(A * C); 
    double RadiceDelta = sqrt(Delta); 
    String Risultato = String.valueOf(RadiceDelta); 

    textview1.setText("Il risultato del deltà è:" + Risultato); 
      setContentView(textview1); 
}  
} 

這是我activity_principale.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".Principale" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="35dp" 
     android:layout_marginTop="18dp" 
     android:text="Inserisci a" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView1" 
     android:layout_below="@+id/textView1" 
     android:ems="10" 
     android:inputType="numberSigned" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/editText1" 
     android:layout_below="@+id/editText1" 
     android:text="Inserisci b" /> 

    <EditText 
     android:id="@+id/editText2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView2" 
     android:layout_below="@+id/textView2" 
     android:ems="10" 
     android:inputType="numberSigned" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/editText2" 
     android:layout_below="@+id/editText2" 
     android:text="Inserisci c" /> 

    <EditText 
     android:id="@+id/editText3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView3" 
     android:layout_below="@+id/textView3" 
     android:ems="10" 
     android:inputType="number" > 

     <requestFocus /> 
    </EditText> 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/textView3" 
     android:layout_below="@+id/editText3" 
     android:layout_marginTop="46dp" 
     android:text="" /> 

    <TextView 
     android:id="@+id/textView5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/editText3" 
     android:layout_alignTop="@+id/textView4" 
     android:text="TextView" /> 

</RelativeLayout> 

當我啓動它,logcat的說:

12-18 20:50:12.278: D/AndroidRuntime(326): Shutting down VM 
12-18 20:50:12.278: W/dalvikvm(326): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
12-18 20:50:12.288: E/AndroidRuntime(326): FATAL EXCEPTION: main 
12-18 20:50:12.288: E/AndroidRuntime(326): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gabriele.progetti/com.gabriele.progetti.Principale}: java.lang.NullPointerException 
12-18 20:50:12.288: E/AndroidRuntime(326): android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
12-18 20:50:12.288: E/AndroidRuntime(326): android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
12-18 20:50:12.288: E/AndroidRuntime(326): android.app.ActivityThread.access$1500(ActivityThread.java:117) 
12-18 20:50:12.288: E/AndroidRuntime(326): android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
12-18 20:50:12.288: E/AndroidRuntime(326): android.os.Handler.dispatchMessage(Handler.java:99) 
12-18 20:50:12.288: E/AndroidRuntime(326): android.os.Looper.loop(Looper.java:123) 
12-18 20:50:12.288: E/AndroidRuntime(326): android.app.ActivityThread.main(ActivityThread.java:3683) 
12-18 20:50:12.288: E/AndroidRuntime(326): java.lang.reflect.Method.invokeNative(Native Method) 
12-18 20:50:12.288: E/AndroidRuntime(326): java.lang.reflect.Method.invoke(Method.java:507) 
12-18 20:50:12.288: E/AndroidRuntime(326): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
12-18 20:50:12.288: E/AndroidRuntime(326): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
12-18 20:50:12.288: E/AndroidRuntime(326): dalvik.system.NativeStart.main(Native Method) 
12-18 20:50:12.288: E/AndroidRuntime(326): Caused by: java.lang.NullPointerException 
12-18 20:50:12.288: E/AndroidRuntime(326): com.gabriele.progetti.Principale.Calcoliamo(Principale.java:45) 
12-18 20:50:12.288: E/AndroidRuntime(326): com.gabriele.progetti.Principale.onCreate(Principale.java:24) 
12-18 20:50:12.288: E/AndroidRuntime(326): android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
12-18 20:50:12.288: E/AndroidRuntime(326): android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
12-18 20:50:12.288: E/AndroidRuntime(326): ... 11 more 12-18 
20:50:14.208: I/Process(326): Sending signal. PID: 326 SIG: 9 
+4

Principale.java第45行發生空指針異常。那是什麼?看這裏。 – Joel

+0

您還沒有初始化textview1 – Joel

+0

我有textview1.setText(「金日成risultato德爾的ΔE:」 + Risultato),但如果我刪除它應用程序崩潰反正。 – Eulante

回答

0

您需要先

if(edittext1.getText().toString()!=null && 
     edittext1.getText().toString().length() !=0){ 
    A = Integer.parseInt(edittext1.getText().toString()); 
    } 
    else{ 
      // set default value here 
       A=0; 
     } 
    if(edittext1.getText().toString()!=null && 
      edittext1.getText().toString().length() !=0){ 
     B = Integer.parseInt(edittext2.getText().toString()); 
     } 
    else{ 
      // set default value here 
      B=0; 
     } 
    if(edittext1.getText().toString()!=null && 
     edittext1.getText().toString().length() !=0){ 
     C = Integer.parseInt(edittext3.getText().toString()); 
    } 
    else{ 
      // set default value here 
       C=0; 
     } 
    //your code here 
:它解析爲Integer如之前檢查的EditText爲null或空
+0

啊,但你並不需要檢查這一點,如果你初始化所有在onCreate()方法,元素的,他應該在這裏做 – Joel

+0

'.getText()。的toString()'部分永遠不會爲空。相反,'edittext1!= null'是需要檢查的。 – Eric

+0

@Eric:您是否可以仔細查看代碼,他正在調用Calcoliamo()方法創建並將EditText值解析爲整數。 100%機會值爲空或爲空 –

4

textview1當你調用textview1.setText()沒有設置,你需要做的

texttext1 = (TextView) findViewById(R.id.textView1); 

爲了防止其他錯誤,它是檢查findViewbyId的結果,以確保它們不爲空使用它們之前是個好主意。

+0

我改變了你說的,但結果是textview1 =(EditText)findViewById(R.id.textView5)。順便說一下,程序仍然崩潰。 – Eulante

+0

我的部分中的剪切和粘貼錯誤應該轉換爲TextView,請參閱上面的編輯文本。 – iagreen

+0

@ user109246:程序仍然崩潰,直到您還沒有實現我的答案也與此答案 –