0

我是 Android 新手,尝试启动应用程序时出现此错误

我会附上一些代码,也许你可以帮助我我的代码有什么问题吗????

$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>
4

1 回答 1

0

你还没有初始化btn_total

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

您可能会遇到空指针异常

  edtxt_total.setText(String.value(total)); 
于 2013-06-21T18:39:42.657 回答