杀死一个活动并启动另一个活动时,Android应用程序崩溃

时间:2014-04-30 13:00:50

标签: java android eclipse

我在Scene2.java设置了一个按钮。我想使用该按钮进入其他活动Scene3.javaGameOver.java一切正常,直到即将开启新活动,每次应用程序在那里崩溃。我想知道我在连接中是否有任何错误,我的意思是newIntent getIntentScene2.java中的GameOver.javaScene3.java

Scene2.java

package com.group5.littlered;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Scene2 extends Activity {

    MediaPlayer bird;
    MediaPlayer bgm;

    int position = 0;
    String[] conversation;
    TextView frame;
    ImageView conframe;
    final String[] ListStr = { "Wake up and ask her", "Peek her secretly" };
    int plot = 0;

    @Override
    public void onBackPressed() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        // Remove title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        // Remove notification bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_scene2);
        Intent intent1 = getIntent();

        conversation = getResources().getStringArray(R.array.scene2);
        frame = (TextView) findViewById(R.id.textView1);

        Button next = (Button) findViewById(R.id.wtf);
        next.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (position < 2) {

                    String sentence = conversation[position];

                    frame.setText(sentence + "");
                    position++;
                } else {
                    if (plot < 1) {
                        AlertDialog choice = new AlertDialog.Builder(
                                Scene2.this).create();
                        choice.setTitle("Pick a choice");
                        choice.setMessage("   ");
                        choice.setButton("Get up and ask her what happened",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog,
                                            int which) {
                                        // TODO Auto-generated method stub
                                        plot = 1;
                                    }
                                });

                        choice.setButton2("Peek her secretly",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog,
                                            int which) {
                                        // TODO Auto-generated method stub

                                        plot = 2;
                                        position = 4;
                                    }
                                });

                        choice.show();

                    } else {

                        if (plot < 2) {
                            if (position < 4) {
                                String sentence = conversation[position];
                                frame.setText(sentence + "");
                                position++;
                            } else {
                                Intent intent2 = new Intent(Scene2.this,
                                        GameOver.class);
                                startActivity(intent2);
                                finish();
                            }
                        } else {
                            if (position < 6) {
                                String sentence = conversation[position];
                                frame.setText(sentence + "");
                                position++;
                            } else {
                                Intent intent3 = new Intent(Scene2.this,
                                        Scene3.class);
                                startActivity(intent3);
                                finish();

                            }
                        }
                    }

                }
            }

        });

        // BGM
        bgm = MediaPlayer.create(Scene2.this, R.raw.voyager);
        bgm.setLooping(true);
        bgm.start();

        // bird
        bird = MediaPlayer.create(Scene2.this, R.raw.bird);
        bird.setLooping(false);
        bird.start();

    }
}

Scene3.java

package com.group5.littlered;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class Scene3 extends Activity {


    int position = 0;
    String[] conversation;
    TextView frame;
    ImageView conframe;

    @Override
    public void onBackPressed() {


    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        // Remove title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        // Remove notification bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_scene3);
        Intent intent3 = getIntent();

        conversation = getResources().getStringArray(R.array.scene1);
        frame = (TextView) findViewById(R.id.textView1);

        Button next = (Button) findViewById(R.id.wtf);
        next.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (position < 6) {

                    String sentence = conversation[position];

                    frame.setText(sentence + "");
                    position++;
                } else {




                    {

                    }

                }
            }

        });



    }

}

再次对不起我可怜的英语,请告诉我我需要发布更多内容以帮助您理解我的问题。

我的logcat

04-30 09:37:39.497: E/AndroidRuntime(4862): FATAL EXCEPTION: main
04-30 09:37:39.497: E/AndroidRuntime(4862): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.group5.littlered/com.group5.littlered.Scene3}: java.lang.NullPointerException
04-30 09:37:39.497: E/AndroidRuntime(4862):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
04-30 09:37:39.497: E/AndroidRuntime(4862):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
04-30 09:37:39.497: E/AndroidRuntime(4862):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-30 09:37:39.497: E/AndroidRuntime(4862):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
04-30 09:37:39.497: E/AndroidRuntime(4862):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 09:37:39.497: E/AndroidRuntime(4862):     at android.os.Looper.loop(Looper.java:137)
04-30 09:37:39.497: E/AndroidRuntime(4862):     at android.app.ActivityThread.main(ActivityThread.java:5103)
04-30 09:37:39.497: E/AndroidRuntime(4862):     at java.lang.reflect.Method.invokeNative(Native Method)
04-30 09:37:39.497: E/AndroidRuntime(4862):     at java.lang.reflect.Method.invoke(Method.java:525)
04-30 09:37:39.497: E/AndroidRuntime(4862):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-30 09:37:39.497: E/AndroidRuntime(4862):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-30 09:37:39.497: E/AndroidRuntime(4862):     at dalvik.system.NativeStart.main(Native Method)
04-30 09:37:39.497: E/AndroidRuntime(4862): Caused by: java.lang.NullPointerException
04-30 09:37:39.497: E/AndroidRuntime(4862):     at com.group5.littlered.Scene3.onCreate(Scene3.java:45)
04-30 09:37:39.497: E/AndroidRuntime(4862):     at android.app.Activity.performCreate(Activity.java:5133)
04-30 09:37:39.497: E/AndroidRuntime(4862):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-30 09:37:39.497: E/AndroidRuntime(4862):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
04-30 09:37:39.497: E/AndroidRuntime(4862):     ... 11 more

3 个答案:

答案 0 :(得分:1)

崩溃的行是Scene3的第45行:

Button next = (Button) findViewById(R.id.wtf);  
next.setOnClickListener(new View.OnClickListener() { // <-- THIS ONE
    ...
});

原因是NullPointerException。这意味着标识符&#34; wtf&#34;存在于R(否则无法编译),但在activity_scene3图层中找不到,因为我们在Scene3.onCreate()的下面的第38行声明:

setContentView(R.layout.activity_scene3); // and later on findViewById() returns `null`

您必须重新访问此布局,以确保您愿意访问的Button实际存在,ID为wtf

一般来说,这是在不同布局中使用相同ID的危险。这很容易隐藏容易被发现的错误,否则就不会编译。

答案 1 :(得分:0)

检查清单文件并在其中添加Scene3.java

<activity 
android:name=".Scene3" >
</activity>

答案 2 :(得分:0)

始终发布有异常的问题,其次可能是您没有在清单文件中提及您的其他活动,如:

<activity
android:name=".Scene3">
</activity>


<activity
    android:name=".GameOver">
    </activity>