2017-09-25 38 views
-1

我不明白爲什麼我的「應用程序」已停止工作?應用停止運行的Android

這是我的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> 

每當我上運行的Android Genymotion這個應用程序:6.0.0和API 23,它正顯示出「不幸的是,(應用程序名稱)不工作」

什麼是我的代碼/平臺的問題?

我試着重新啓動我的應用程序,擡頭幾個問題但仍可能得到我的答案,當我使用意向打開一個新的活動,第二個活動不能解析xml文件。

+3

堆棧跟蹤將是很有益這裏 – Neeraj

+0

請看看這個:https://stackoverflow.com/help/mcve要求在崩潰的幫助,當 – GuilhermeFGL

+1

發表您的堆棧跟蹤。否則,我們猜測的問題是什麼,甚至有可能不止一個。 –

回答

2

你的應用程序崩潰的源頭是以下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(); 
            } 
           }); 
        } 
       } 
     ); 
    } 
}