0

このアプリケーションの作業を始めたばかりですが、すべてが正しく行われたように感じます(これは、私が作業した最初のアプリケーションではありません)が、実行しようとするとクラッシュしますか?エラーは発生しません。

これがメインアクティビティからのコード、次にmanufestからのコード、そしてxmlレイアウトからのコードです。`

     package com.theory.game;
     import java.util.Random;
     import android.app.Activity;
     import android.content.Intent;
     import android.os.Bundle;
     import android.view.View;
     import android.widget.Button;

    public class GameTheoryActivity extends Activity implements View.OnClickListener{

    Button play, settings;
    int i;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        play.setOnClickListener(this);
        settings.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case R.id.bPlay:

            Random irand = new Random();
            i = irand.nextInt(4);


            switch(i){
            case 0:

                Intent GoMillionair = new Intent(GameTheoryActivity.this, millionair.class);
                startActivity(GoMillionair);
                return;
            case 1:
                return;
            case 2:

                return;
            case 3:

                return; 
            }

            return;
        case R.id.bSettings:
            return; 
        }
    }
}

マニュフェスト:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.theory.game"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application >
        <activity
            android:name=".GameTheoryActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".millionair" >
            <intent-filter>
                <action android:name="com.theory.game.MILLIONAIR" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/backgroundimage"
    android:orientation="vertical" >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="390dp"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >


        <Button
            android:id="@+id/bPlay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.38"
            android:background="@drawable/play" />

        <Button
            android:id="@+id/bSettings"
            android:layout_width="match_parent"
            android:layout_height="58dp"
            android:layout_weight="0.36"
            android:background="@drawable/settings" />
    </LinearLayout>

</LinearLayout> 

`

そして、私にはミリオンエアと呼ばれる別のクラスがあります。これは通常のクラスであり、何も悪いことではないと思います。これがどうなっているのかを確認して、アプリケーションが「動作を停止した」と表示し始めない理由を教えてください。ありがとうございます。

4

3 に答える 3

1

playオブジェクトとsettingsオブジェクトの両方が nullであるため、NullPointerException が発生していると思います。

onCreate() を次のように変更します。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    play = (Button)findViewById(R.id.bPlay);
    settings = (Button)findViewById(R.id.bSettings);

    play.setOnClickListener(this);
    settings.setOnClickListener(this);

}
于 2012-07-02T23:35:07.180 に答える
1

NullPointerExceptionボタンをフィールドに割り当てるのを忘れたため、おそらく が表示されます ( Button play, settings;)。

それらを割り当てる方法の例を次に示します。

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    play = (Button) findViewById(R.id.bPlay);
    settings = (Button) findViewById(R.id.bSettings);
}
于 2012-07-02T23:39:05.473 に答える
0

onCreate() メソッドで使用しようとすると、 play と settings の両方が null になるように見えます。

play.setOnClickListener(this);
settings.setOnClickListener(this);

次のように開始してください。

play = (Button)findViewById(R.id.bPlay);
settings = (Button)findViewById(R.id.bSettings)
play.setOnClickListener(this);
settings.setOnClickListener(this);
于 2012-07-02T23:35:14.823 に答える