2012-05-31 89 views
0

我是Android新手。 當我運行我的應用程序,它在橫向模式下運行良好,但在縱向模式下,應用程序被強制關閉,從而在日誌下面Android應用程序在肖像模式下強行關閉,但在橫向上運行良好

05-31 16:48:30.958: W/KeyCharacterMap(428): No keyboard for id 0 
05-31 16:48:30.958: W/KeyCharacterMap(428): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 
05-31 16:50:45.728: D/AndroidRuntime(484): Shutting down VM 
05-31 16:50:45.728: W/dalvikvm(484): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
05-31 16:50:45.758: E/AndroidRuntime(484): FATAL EXCEPTION: main 
05-31 16:50:45.758: E/AndroidRuntime(484): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kalmas/com.kalmas.Kalma2}: java.lang.NullPointerException 
05-31 16:50:45.758: E/AndroidRuntime(484): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
05-31 16:50:45.758: E/AndroidRuntime(484): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
05-31 16:50:45.758: E/AndroidRuntime(484): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
05-31 16:50:45.758: E/AndroidRuntime(484): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
05-31 16:50:45.758: E/AndroidRuntime(484): at android.os.Handler.dispatchMessage(Handler.java:99) 
05-31 16:50:45.758: E/AndroidRuntime(484): at android.os.Looper.loop(Looper.java:123) 
05-31 16:50:45.758: E/AndroidRuntime(484): at android.app.ActivityThread.main(ActivityThread.java:4627) 
05-31 16:50:45.758: E/AndroidRuntime(484): at java.lang.reflect.Method.invokeNative(Native Method) 
05-31 16:50:45.758: E/AndroidRuntime(484): at java.lang.reflect.Method.invoke(Method.java:521) 
05-31 16:50:45.758: E/AndroidRuntime(484): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
05-31 16:50:45.758: E/AndroidRuntime(484): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
05-31 16:50:45.758: E/AndroidRuntime(484): at dalvik.system.NativeStart.main(Native Method) 
05-31 16:50:45.758: E/AndroidRuntime(484): Caused by: java.lang.NullPointerException 
05-31 16:50:45.758: E/AndroidRuntime(484): at com.kalmas.Kalma2.onCreate(Kalma2.java:52) 
05-31 16:50:45.758: E/AndroidRuntime(484): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
05-31 16:50:45.758: E/AndroidRuntime(484): at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
05-31 16:50:45.758: E/AndroidRuntime(484): ... 11 more 
05-31 16:50:48.108: I/Process(484): Sending signal. PID: 484 SIG: 9 

我Kalma2.java的代碼是

package com.kalmas; 

import android.app.Activity; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ImageView; 

public class Kalma2 extends Activity { 

    public void onCreate(Bundle savedInstanceState) { 
     final MediaPlayer kalma2 = MediaPlayer.create(this, R.raw.kalma2); 
     super.onCreate(savedInstanceState); 
    setContentView(R.layout.kalma2); 

    ImageView prev2 = (ImageView)findViewById(R.id.prev2); 

    prev2.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      kalma2.stop(); 
      startActivity(new Intent(Kalma2.this,Kalma1.class)); 
     } 
    }); 

    ImageView next2 = (ImageView)findViewById(R.id.next2); 
    next2.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      kalma2.stop(); 
      startActivity(new Intent(Kalma2.this,Kalma3.class)); 
     } 
    }); 

    ImageView pause = (ImageView)findViewById(R.id.pause2); 
    pause.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      kalma2.pause(); 
     } 
    }); 

    ImageView play = (ImageView)findViewById(R.id.play2); 
    play.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      kalma2.start(); 
     } 
    }); 

    ImageView kalma = (ImageView)findViewById(R.id.kalma2); 

    kalma.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      kalma2.start(); 
     } 
    }); 



} 

} 
+0

這是當你從風景切換到肖像或當你剛開始你的應用程序的肖像? – Blundell

+0

發佈了代碼Kalma2.java –

+0

當我啓動應用程序時,我從1個屏幕移動到另一個屏幕,在第二個屏幕上,它正在強制關閉 – baig772

回答

1

據我所知,在第52行,您嘗試將一個點擊偵聽器附加到ImageView上。

ImageView play = (ImageView)findViewById(R.id.play2); 
play.setOnClickListener(new OnClickListener() { ... 

您確定您的佈局包含ID爲play2的ImageView嗎?會很高興有kalma2.xml的代碼太:)

+0

是的,這是錯誤,現在它的運行良好:) – baig772

0

沒有看到您的佈局並確切知道第52行是什麼,很難說,但最可能的問題是您的findViewById調用之一返回null,並且當您使用它來嘗試並設置clickListener時,您會得到NPE 。

檢查ID並確保您匹配大小寫以及ID。

相關問題