Android应用程序启动时崩溃

时间:2012-12-18 19:56:00

标签: android string crash android-edittext logcat

我已经提出了这个申请,但是当我尝试午餐时,他立即关闭了。 这是我的Principale.java:

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

2 个答案:

答案 0 :(得分:4)

当您致电textview1时,

textview1.setText()未设置,您需要

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

为防止出现其他错误,最好在使用它们之前检查findViewbyId的结果,以确保它们不为空。

答案 1 :(得分:0)

您需要首先检查EditText是否为Null或Empty,然后将其解析为Integer:

   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