为什么我的应用显示"该应用程序意外停止"?

时间:2016-02-22 12:59:22

标签: android

我创建了一个应用。当我运行Android Studio 2.0 beta 5模拟器时,它运行得很好。但是,当我运行原始设备时,它显示"该应用程序意外停止"。   这里面的问题是什么?

public class MainActivity extends AppCompatActivity {
final Context context=this;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public void onClick(View v){
    switch (v.getId()){
        case R.id.button1:
                            final Dialog dialog1=new Dialog(context);
            dialog1.setContentView(R.layout.dialog);
            Button finish1= (Button) dialog1.findViewById(R.id.finish);
            finish1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog1.dismiss();
                }
            });
            dialog1.show();
            break;
        case R.id.button2:
                           final Dialog dialog2=new Dialog(context);
            dialog2.setContentView(R.layout.dialog);
            Button finish2= (Button) dialog2.findViewById(R.id.finish);
            finish2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog2.dismiss();
                }
            });
            dialog2.show();
            break;
        case R.id.button3:
                           final Dialog dialog3=new Dialog(context);
            dialog3.setContentView(R.layout.dialog);
            Button button3= (Button) dialog3.findViewById(R.id.finish);
            button3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog3.dismiss();
                }
            });
            dialog3.show();
            break;
        default:
            break;
    }
}
}

这是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/colorPrimary"
android:orientation="vertical"
tools:context="com.example.akibmahmud.animalsanddrawing.MainActivity">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Wellcome Baby!!!!!!!"
    android:textColor="@color/colorAccent"
    android:textSize="30dp" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"
    android:background="@color/colorPrimary"
    android:onClick="onClick"
    android:text="Dialog1"
    android:textColor="@color/colorAccent"
    android:textSize="10dp" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/button1"
    android:layout_marginTop="56dp"
    android:background="@color/colorPrimary"
    android:onClick="onClick"
    android:text="Dialog2"
    android:textColor="@color/colorAccent"
    android:textSize="10dp" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/button2"
    android:layout_toEndOf="@+id/textView"
    android:layout_toRightOf="@+id/textView"
    android:background="@color/colorPrimary"
    android:onClick="onClick"
    android:text="Dialog3"
    android:textColor="@color/colorAccent"
    android:textSize="10dp" />
    </LinearLayout>

它在模拟器中完美运行。

1 个答案:

答案 0 :(得分:-1)

在使用之前正确引用您的按钮

public class MainActivity extends AppCompatActivity {
    final Context context=this;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  Button mButton1= (Button) findViewById(R.id.button1);
  mButton1.setOnClickListener(this); 

 .... // the same to all buttons 
}
public void onClick(View v){
    switch (v.getId()){
        case R.id.button1:
                            final Dialog dialog1=new Dialog(context);
            dialog1.setContentView(R.layout.dialog);
            Button finish1= (Button) dialog1.findViewById(R.id.finish);
            finish1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog1.dismiss();
                }
            });
            dialog1.show();
            break;
        case R.id.button2:
                           final Dialog dialog2=new Dialog(context);
            dialog2.setContentView(R.layout.dialog);
            Button finish2= (Button) dialog2.findViewById(R.id.finish);
            finish2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog2.dismiss();
                }
            });
            dialog2.show();
            break;
        case R.id.button3:
                           final Dialog dialog3=new Dialog(context);
            dialog3.setContentView(R.layout.dialog);
            Button button3= (Button) dialog3.findViewById(R.id.finish);
            button3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog3.dismiss();
                }
            });
            dialog3.show();
            break;
        default:
            break;
    }
}
}