2017-09-25 4 views
-1

「アプリ」が動作を停止した理由を理解できません。アプリの動作が停止しました

これは私のMainActivity.classコードです: - 私はGenymotion Android上でこのアプリを実行しているときはいつでも

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> 

:6.0.0およびAPI 23を、それが「残念ながら、(アプリ名)は機能していません」

私のコード/プラットフォームの問題は何ですか?

私はアプリを再起動しようとしましたが、いくつかの質問を見ましたが、意図を使って新しいアクティビティを開いたときに私の答えを得ることができなかった、2番目のアクティビティは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(); 
            } 
           }); 
        } 
       } 
     ); 
    } 
} 
関連する問題