1
我有以下代碼:無法實例活動ComponentInfo {...}:顯示java.lang.NullPointerException和一些其他的東西
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.app.Activity;
public class MainActivity extends Activity
{
MediaPlayer m3;
MediaPlayer m2;
MediaPlayer m1;
MediaPlayer m4;
ImageView im2 = (ImageView) findViewById(R.id.im2);
ImageView im3 = (ImageView) findViewById(R.id.im3);
ImageView im4 = (ImageView) findViewById(R.id.im4);
ImageView im5 = (ImageView) findViewById(R.id.im5);
Button button;
int a = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m1 = new MediaPlayer();
m1 = MediaPlayer.create(this, R.raw.m1);
m1.setAudioStreamType(AudioManager.STREAM_MUSIC);
m2 = new MediaPlayer();
m2 = MediaPlayer.create(this, R.raw.m2);
m2.setAudioStreamType(AudioManager.STREAM_MUSIC);
m3 = new MediaPlayer();
m3 = MediaPlayer.create(this, R.raw.m3);
m3.setAudioStreamType(AudioManager.STREAM_MUSIC);
m4 = new MediaPlayer();
m4 = MediaPlayer.create(this, R.raw.m4);
m4.setAudioStreamType(AudioManager.STREAM_MUSIC);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (a == 0)
{
m1.start();
im2.setImageResource(R.drawable.i2);
a = 1;
}
else if (a == 1)
{ m2.start();
im3.setImageResource(R.drawable.i3);
a = 2;
}
else if (a == 2)
{ m3.start();
im4.setImageResource(R.drawable.i4);
a = 3;
}
else if (a == 3)
{ m4.start();
im5.setImageResource(R.drawable.i5);
a = 4;
}
}
});
}
}
但它有一個錯誤發射嘗試後馬上崩潰。以下是我在logcat中看到:
03-19 22:14:58.232: E/AndroidRuntime(29846): FATAL EXCEPTION: main
03-19 22:14:58.232: E/AndroidRuntime(29846): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.luckyyou.justin/com.luckyyou.justin.MainActivity}: java.lang.NullPointerException
03-19 22:14:58.232: E/AndroidRuntime(29846): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
03-19 22:14:58.232: E/AndroidRuntime(29846): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
03-19 22:14:58.232: E/AndroidRuntime(29846): at android.app.ActivityThread.access$600(ActivityThread.java:162)
03-19 22:14:58.232: E/AndroidRuntime(29846): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
03-19 22:14:58.232: E/AndroidRuntime(29846): at android.os.Handler.dispatchMessage(Handler.java:107)
03-19 22:14:58.232: E/AndroidRuntime(29846): at android.os.Looper.loop(Looper.java:194)
03-19 22:14:58.232: E/AndroidRuntime(29846): at android.app.ActivityThread.main(ActivityThread.java:5371)
03-19 22:14:58.232: E/AndroidRuntime(29846): at java.lang.reflect.Method.invokeNative(Native Method)
03-19 22:14:58.232: E/AndroidRuntime(29846): at java.lang.reflect.Method.invoke(Method.java:525)
03-19 22:14:58.232: E/AndroidRuntime(29846): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
03-19 22:14:58.232: E/AndroidRuntime(29846): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-19 22:14:58.232: E/AndroidRuntime(29846): at dalvik.system.NativeStart.main(Native Method)
03-19 22:14:58.232: E/AndroidRuntime(29846): Caused by: java.lang.NullPointerException
03-19 22:14:58.232: E/AndroidRuntime(29846): at android.app.Activity.findViewById(Activity.java:1839)
03-19 22:14:58.232: E/AndroidRuntime(29846): at com.luckyyou.justin.MainActivity.<init>(MainActivity.java:21)
03-19 22:14:58.232: E/AndroidRuntime(29846): at java.lang.Class.newInstanceImpl(Native Method)
03-19 22:14:58.232: E/AndroidRuntime(29846): at java.lang.Class.newInstance(Class.java:1319)
03-19 22:14:58.232: E/AndroidRuntime(29846): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
03-19 22:14:58.232: E/AndroidRuntime(29846): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2260)
03-19 22:14:58.232: E/AndroidRuntime(29846): ... 11 more
03-19 22:14:58.292: E/AppErrorDialog(497): Failed to get ILowStorageHandle instance
我沒宣佈我的清單活動,確實應該讓我的應用程序工作,其他的東西,但它沒有,所以我不知道該怎麼辦,請大家幫忙!
哦謝謝!愚蠢的錯誤:) – user3158610
是的,它是。現在,請接受答案來結束問題。 –