我有以下代码:
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;
}
}
});
}
}
但是启动try之后,它立即崩溃并显示错误。这是我在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
我确实在Manifest中声明了我的活动,并做了其他一些应能使我的应用程序正常运行的工作,但没有成功,所以我不知道该怎么办,请帮忙!
最佳答案
既然如此,您甚至在Layout
设置为活动之前就尝试查找视图,这就是为什么跟随Exception
发生的原因...
java.lang.NullPointerException at android.app.Activity.findViewById(Activity.java:1839)
初始化
ImageViews
方法中的所有onCreate()
,如下所示...ImageView im2;
ImageView im3;
ImageView im4;
ImageView im5;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
im2 = (ImageView) findViewById(R.id.im2);
im3 = (ImageView) findViewById(R.id.im3);
im4 = (ImageView) findViewById(R.id.im4);
im5 = (ImageView) findViewById(R.id.im5);
}