2013-06-21 103 views
0

我在Android的新的和我有嘗試啓動應用程序Android錯誤:應用程序「appl name」意外停止。請重試

我會重視你一些代碼,當這個錯誤,也許你可以幫我 有什麼問題我的代碼????

$ Java代碼

package digicare.try2; 



import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class MainActivity_try extends Activity { 
int num1, num2 ; 
Button sub,add,btn_total; 
TextView txt_ans ; 
int num_1 , num_2 , total; 
EditText edtxt_num1,edtxt_num2,edtxt_total; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main_activity_try); 
    num1=1; 
    add=(Button) findViewById(R.id.btn_add); 
    sub =(Button) findViewById(R.id.btn_sub); 
    txt_ans=(TextView)findViewById(R.id.textView1); 
    edtxt_num1=(EditText) findViewById(R.id.editText_num1); 
    edtxt_num2=(EditText) findViewById(R.id.editText_num2); 
    edtxt_total=(EditText) findViewById(R.id.editText_total); 


    btn_total.setOnClickListener(new View.OnClickListener() { 


     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      String num1_str = edtxt_num1.getText().toString(); 
      String num2_str=edtxt_num2.getText().toString(); 
      int num1_cal=Integer.parseInt(num1_str); 
      int num2_cal=Integer.parseInt(num2_str); 
     total=num1_cal + num2_cal; 
     edtxt_total.setText(total); 


     } 
    }); 

    add.setOnClickListener(new View.OnClickListener() { 




     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      num1 ++; 
      txt_ans.setText("Your Answer is "+num1); 
     } 
    }); 
    sub.setOnClickListener(new View.OnClickListener() { 


     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      num1--; 
      txt_ans.setText("Your total is "+num1); 

     } 
    }); 

} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main_activity_try, menu); 
    return true; 
} 

} 

$ 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=".MainActivity_try" > 

<Button 
    android:id="@+id/btn_add" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:text="@string/btn_name" /> 

<Button 
    android:id="@+id/btn_sub" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/btn_add" 
    android:text="@string/btn_sub" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/btn_sub" 
    android:text="@string/txt" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<EditText 
    android:id="@+id/editText_total" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/button1" 
    android:ems="10" 
    android:inputType="number" /> 

<EditText 
    android:id="@+id/editText_num1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView1" 
    android:layout_marginLeft="129dp" 
    android:layout_marginTop="117dp" 
    android:layout_toRightOf="@+id/editText_total" 
    android:ems="10" 
    android:inputType="number" /> 

<TextView 
    android:id="@+id/textView_num1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/editText_num1" 
    android:layout_alignLeft="@+id/editText_num1" 
    android:layout_marginBottom="50dp" 
    android:text="@string/num1" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<EditText 
    android:id="@+id/editText_num2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView1" 
    android:layout_marginLeft="56dp" 
    android:layout_toRightOf="@+id/editText_num1" 
    android:ems="10" 
    android:inputType="number" > 

    <requestFocus /> 
</EditText> 

<TextView 
    android:id="@+id/textView_num2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/textView1" 
    android:layout_marginLeft="30dp" 
    android:layout_toRightOf="@+id/editText_num1" 
    android:text="@string/num2" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/editText_num1" 
    android:layout_alignBottom="@+id/editText_num1" 
    android:layout_alignLeft="@+id/editText_num2" 
    android:layout_marginLeft="43dp" 
    android:text="@string/btn_total" /> 

</RelativeLayout> 

$ XML字符串

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

<string name="app_name">Try</string> 
<string name="hello_world">Hello world!</string> 
<string name="menu_settings">Settings</string> 
<string name="btn_name">add one </string> 
<string name="btn_sub">minus one</string> 
<string name="txt">your total is zero</string> 
<string name="num1">1st Number</string> 
<string name="num2">Second Number</string> 
<string name="total">Total</string> 
<string name="btn_total">Check Total</string> 

</resources> 
+0

檢查清單... – stinepike

+0

閱讀您的logcat的。它可能會告訴你失敗的確切行(文件和行號)。 – mah

+0

或者logcat:附加一個調試器並停止異常。我敢打賭,一美元就有一美元。 – user2246674

回答

0

您還沒有初始化btn_total

btn_total =(Button) findViewById(R.id.button1); 

你可能會流汗空指針異常

而且

edtxt_total.setText(String.value(total)); 
+0

在哪裏寫這個「edtxt_total.setText(String.value(total));」 – natiq

+0

裏面'btn_total'點擊監聽器 – Raghunandan

+0

@ user2185027讓我知道它是否工作。 – Raghunandan

相關問題