“不幸的是应用程序停止了”错误在运行应用程序时在eclipse中弹出。我正在使用API​​ 17

时间:2013-08-01 20:18:45

标签: android

"unfortunately application has stopped" error poping up in eclipse while running app. i am using API17, i did everything. but still i have same problem...this bug wasted my whole day.     

这是activity_main.xml。我犯了这个bug.pls帮助了一些......我用过textview和两个按钮......但是他们给出了一个关于视图包的错误。所以我清理项目......但现在它告诉我应用程序已停止。

    package com.example.savetouch;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;

    public class MainActivity extends Activity {
        int counter;
        Button pl,op;
        TextView display;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            counter=0;
            pl=(Button) findViewById(R.id.bplay);
            op=(Button) findViewById(R.id.boption);
            display=(Button) findViewById(R.id.tvdisplay);
            pl.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    counter++;
                    display.setText(""+counter);
                }
            });
            op.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    counter--;
                }
            });
        }

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


    }

it giving problem in view pakage, and text pakage.. to solve that problem i have imported android.view.view.OnClickListener.



This is activity_main.xml. gone mad by this bug.pls help some one......i have used on textview, and two buttons..but they was giving an error about view package. so i clean the project...but now it showing me that application has stopped.


<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"
    android:gravity="center"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tvdisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="SAVETOUCH"
        android:textSize="45sp" />

    <Button
        android:id="@+id/boption"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/tvdisplay"
        android:layout_below="@+id/bplay"
        android:text="Option"
        android:textSize="20sp" />

    <Button
        android:id="@+id/bplay"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/boption"
        android:layout_below="@+id/tvdisplay"
        android:layout_marginTop="22dp"
        android:text="Play"
        android:textSize="20sp" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

变化:

display=(Button) findViewById(R.id.tvdisplay);

display=(TextView) findViewById(R.id.tvdisplay);

您已将display声明为TextView,并且您的ID似乎也表明它是TextView。

答案 1 :(得分:0)

您可能正在使用 ClassCastException 。为什么呢?

您的XML已声明TextView,其ID为tvdisplay,但在Activity方面,您将该视图转换为Button,这就是您的例外来源。

要解决您的问题,请简单替换您的行:

display=(Button) findViewById(R.id.tvdisplay);

display=(TextView) findViewById(R.id.tvdisplay);