应用程序停止工作android

时间:2017-09-25 18:18:58

标签: android

我不明白我的" App"已停止工作了?

这是我的MainActivity.class代码: -

package com.apps.nishant.iwillguessyournumber;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button btnplay = (Button)findViewById(R.id.cmdPlay);
    private Button btnexit = (Button)findViewById(R.id.cmdExit);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        OnButtonClickListener();
    }

    public void OnButtonClickListener() {
        btnplay.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                }
        );

        btnexit.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
                        adb.setMessage("Do you really wanna exit?")
                                .setCancelable(false)
                                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        finish();
                                    }
                                })
                                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.cancel();
                                    }
                                });
                    }
                }
        );
    }
}

和我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.apps.nishant.iwillguessyournumber.MainActivity">

    <Button
        android:id="@+id/cmdPlay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Play"
        tools:layout_editor_absoluteX="147dp"
        tools:layout_editor_absoluteY="219dp" />

    <Button
        android:id="@+id/cmdExit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Exit"
        tools:layout_editor_absoluteX="147dp"
        tools:layout_editor_absoluteY="328dp" />
</android.support.constraint.ConstraintLayout>

每当我在Genymotion Android:6.0.0和API 23上运行此应用程序时,它显示&#34;不幸的是,(应用程序名称)无效&#34;

我的代码/平台有什么问题?

我尝试重新启动我的应用程序,查找了一些问题,但是当我使用Intent打开一个新的Activity时,我还没有得到我的答案,第二个活动无法解析xml文件。

1 个答案:

答案 0 :(得分:2)

您的应用程序崩溃的来源是以下两行,

private Button btnplay = (Button)findViewById(R.id.cmdPlay);
private Button btnexit = (Button)findViewById(R.id.cmdExit);

方法findViewById()应该在setContentView()之后的onCreate()内部调用。以下代码应该可以使用,

package com.apps.nishant.iwillguessyournumber;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button btnplay;
    private Button btnexit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnplay = (Button)findViewById(R.id.cmdPlay);
        btnexit = (Button)findViewById(R.id.cmdExit);
        OnButtonClickListener();
    }

    public void OnButtonClickListener() {
        btnplay.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                }
        );

        btnexit.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
                        adb.setMessage("Do you really wanna exit?")
                                .setCancelable(false)
                                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        finish();
                                    }
                                })
                                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.cancel();
                                    }
                                });
                    }
                }
        );
    }
}