Eclipse Android App运行时错误

时间:2014-05-23 10:29:33

标签: java android eclipse

我是通过Eclipse开发Android应用程序的新手...我使用2个按钮和文本视图创建了一个非常简单的应用程序...这样如果我单击添加按钮...文本视图将添加一个数字(如0到1,1对2等等..)同样,如果我点击减去按钮它会减少数字.. Java和XML文件中没有错误,但是当我尝试运行它时显示错误---应用程序意外停止。请再试一次。强制关闭...

我该怎么做才能让它正常运行.. 期待快速可靠的答案......谢谢。 下面是我的java以及xml编码!!!

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/bg" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="@string/text"
        android:textColor="#fff"
        android:textSize="26sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/badd"
        android:textColor="#ffffff" />

    <Button
        android:id="@+id/button2"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/bsub"
        android:textColor="#ffffff" />

</LinearLayout>



public class MainActivity extends ActionBarActivity {

int count;
Button sum, sub ;
TextView display ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    count = 0;
    sum = (Button) findViewById(R.id.button1);
    sub = (Button) findViewById(R.id.button2);
    display = (TextView) findViewById(R.id.textView1);

    sum.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            count++;
            display.setText("Your Total Count is " + count);            }
    });

    sub.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            count++;
            display.setText("Your Total Count is " + count);            }
    });

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}

3 个答案:

答案 0 :(得分:0)

请试试这个

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_main, container,
  false);

sum = (Button) root.findViewById(R.id.button1);
sub = (Button) root.findViewById(R.id.button2);
display = (TextView) root.findViewById(R.id.textView1);

sum.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        count++;
        display.setText("Your Total Count is " + count);            
 }
});

// the rest of your code

return root;
}

答案 1 :(得分:0)

如果您使用按钮或按钮单击事件等,则需要获取资源。例如,您的按钮ID为button1:

public class MainActivity extends Activity {


    Button button1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


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

....

}

}

我认为你不使用findViewById方法。你能给出你的Java代码吗?

答案 2 :(得分:0)

由于你没有放置你的JAVA文件并且无法发布logcat,我正在展示我几周前用相同/类似概念开发的类似APP。请仔细阅读..

Simple_Maths活动

public class Simple_Maths extends Activity {
    TextView tv_Output;
    Button Addition, Subtract, ClearTot;
    int counter = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView (R.layout.maths_activity);
        tv_Output = (TextView) findViewById(R.id.tv_Display);
        Addition = (Button) findViewById(R.id.btn_Add);
        Subtract = (Button) findViewById(R.id.btn_Sub);
        ClearTot = (Button) findViewById(R.id.btn_Clear);


        Addition.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter++;
                tv_Output.setText("Your Total is : " + counter);
            }
        });

        Subtract.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter--;
                if (counter < 0) {
                    counter = 0;
                }
                tv_Output.setText("Your Total is : " + counter);
            }
        });

        ClearTot.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter = 0;
                tv_Output.setText("Your Total is cleared");
            }
        });
    }


}

maths_activity.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:background="@color/LightOrange"
    android:orientation="vertical"
    android:padding="10dp" >

    <TextView
        android:id="@+id/tv_Display"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@color/Variety"
        android:gravity="center"
        android:hint="@string/tv_Display"
        android:textSize="35sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/LightOrange"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_Add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="10dp"
            android:background="@color/Red"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:text="@string/btn_Add"
            android:textSize="20sp" />

        <Button
            android:id="@+id/btn_Sub"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:background="@color/LightYellow"
            android:text="@string/btn_Sub"
            android:textSize="20sp" />

        <Button
            android:id="@+id/btn_Clear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:background="@color/LightGreen"
            android:text="@string/btn_Clear"
            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/Light_Green"
            android:layout_marginTop="60dp"
            android:padding="10dp"
            android:gravity="center"
            android:text="Hello, Mr. This is in Portrait Mode :>)" />
    </LinearLayout>

</LinearLayout>

确保在清单文件中声明它

如果有任何理解问题,请告诉我