有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java应用程序在尝试新活动时崩溃

我正在安卓上制作一个游戏,我想使用onClickListener中的循环来显示存储在String.xml中的对话框

我设置了一些代码,但应用程序崩溃了,eclipse中没有错误,但我不知道出了什么问题。所以请帮我检查代码。我将展示我可以在没有崩溃的情况下运行的代码(使用onClickListener,不使用循环)和导致崩溃的错误代码(使用我编写的循环,我猜那里有一些错误)

那个不会崩溃的

package com.group5.littlered;

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

public class Scene1 extends Activity{

MediaPlayer wovleshowling;
MediaPlayer bgm;

@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_scene1);
    Intent intent = getIntent();

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

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            }



        }
);

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

    //wovleshowling
    wovleshowling = MediaPlayer.create(Scene1.this, R.raw.wolveshowling);
    wovleshowling.setLooping(false);
    wovleshowling.start();

}

}

坠毁的那一个:

    package com.group5.littlered;

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

public class Scene1 extends Activity{

MediaPlayer wovleshowling;
MediaPlayer bgm;

int position = 0;
String [] conversation = getResources().getStringArray(R.array.scene1);
TextView frame = (TextView) findViewById(R.id.textView1);

@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_scene1);
    Intent intent = getIntent();

    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 ++ ;
            }
            }



        }
);

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

    //wovleshowling
    wovleshowling = MediaPlayer.create(Scene1.this, R.raw.wolveshowling);
    wovleshowling.setLooping(false);
    wovleshowling.start();





}

}

共 (3) 个答案

  1. # 1 楼答案

    您应该在onCreate()中更新UI,因此在onCreate()中将TextView与xml绑定

    public class Scene1 extends Activity{
    
    MediaPlayer wovleshowling;
    MediaPlayer bgm;
    
    int position = 0;
    String [] conversation = getResources().getStringArray(R.array.scene1);
    TextView frame;
    
    @Override
    public void onBackPressed(){}
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
         frame = (TextView) findViewById(R.id.textView1);//this should be inside the onCreate() method
         //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_scene1);
        Intent intent = getIntent();
    
        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 ++ ;
                }
                }
    
    
    
            }
    );
    
        //BGM
        bgm = MediaPlayer.create(Scene1.this, R.raw.unsettledthoughts);
        bgm.setLooping(true);
        bgm.start();
    
        //wovleshowling
        wovleshowling = MediaPlayer.create(Scene1.this, R.raw.wolveshowling);
        wovleshowling.setLooping(false);
        wovleshowling.start();
    
    }
    
    }
    
  2. # 2 楼答案

    TextView框架=(TextView)findViewById(R.id.textView1) 必须位于创建应用程序后调用的方法onCreate()

  3. # 3 楼答案

    请走开

    TextView frame = (TextView) findViewById(R.id.textView1);
    

    setContentView调用后的内部onCreate()

    由于您没有粘贴日志,我假设这会导致onClick()函数中出现NullPointerException