繁体   English   中英

杀死一项活动并开始另一项活动时,android应用程序崩溃

[英]android app crashes when killing one activity and start another one

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

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

崩溃的行是Scene3的第45 Scene3

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

原因是NullPointerException 这意味着标识符“ wtf”存在于R (否则将无法编译),但在activity_scene3层中找不到,因为我们挥动了Scene3.onCreate()的以下语句行38:

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

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

一般来说,在不同布局中使用相同的ID会有危险。 这很容易隐藏错误,否则将很容易发现这些错误,因为它们只是无法编译。

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

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

总是例外地发布问题,其次是您可能没有在清单文件中提及您的其他活动,例如:

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


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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM