2014-04-30 59 views
1

有一個按鈕,我在Scene2.java設置。我要使用的按鈕等活動Scene3.java獲得,GameOver.java一切工作正常,直到其即將開啓新的活動,每該應用在那裏崩潰。我想知道是否有是我在連接的任何錯誤,我指的是在Scene2.javaGameOver.javanewIntentgetIntentScene3.java的Android應用程序崩潰另一個

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 
+0

您是否添加了Scene3和GameOver來顯示?請使用崩潰堆棧跟蹤發佈LogCat輸出。 – Olsavage

+0

你不應該在'startActivity(intent2)'後面調用'finish()'。活動是堆疊的,如果你想阻止用戶從'Scene3'或'GameOver'返回,最好以另一種方式來做。這可能是事故的原因。我無法進一步調查,因爲您沒有發佈與您的崩潰相對應的堆棧跟蹤。 – Shlublu

+0

我會在一段時間上傳我的堆棧跟蹤.plz wait.thanks – user3585596

回答

0

檢查清單文件,並在其中

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

,出現異常後總是添加問題Scene3.java,第二這是可能是你沒有提到你的其他活動清單文件如:

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


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

我在清單中添加了這些內容。還是行不通。也許你可以檢查我的logcat – user3585596

1

崩潰的行是的第45行:

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

原因是NullPointerException。這意味着標識符R「跆拳道」的存在(這本來不會編譯),但在該層activity_scene3是找不到的,因爲我們招手下面的語句行38 Scene3.onCreate()的:

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

你必須重新審視此佈局確保您願意訪問的Button實際存在,ID爲wtf

一般來說,這是在不同佈局中使用相同ID的危險。這很容易隱藏錯誤,否則很容易被發現,因爲這只是不編譯。