当意图进行新活动时,应用程序崩溃

时间:2014-04-29 15:56:36

标签: java android eclipse loops onclicklistener

我在Android上制作游戏,我想在onClickListener中使用循环来显示存储在String.xml

中的对话框

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

不会崩溃的人

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.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 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.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 个答案:

答案 0 :(得分:0)

请移动

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

在setContentView调用之后的onCreate()里面。

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

答案 1 :(得分:0)

你应该在onCreate()中更新UI,在onCreate()中使用xml绑定TextView。

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 :(得分:0)

TextView frame =(TextView)findViewById(R.id.textView1); 必须在 onCreate()方法内,因为在创建应用程序后调用的方法 findViewById()

相关问题