我是初学者,我正在尝试在资源文件夹中创建一个非常简单的应用程序,一个按钮和4个.ogg文件,该按钮将播放上述随机文件。
我只有一个主要类和一个布局。
以下是我所做的,代码没有显示任何错误,但在模拟器上启动时应用程序立即崩溃。
我的主要课程: -
MediaPlayer player;
AssetFileDescriptor descriptor;
private String[] filelist;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item);
player = new MediaPlayer();
Button playSound = (Button)findViewById(R.id.button1);
AssetManager aMan = this.getAssets();
try
{
filelist = aMan.list("/assets");
} catch (IOException e1) {
e1.printStackTrace();
}
playSound.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(filelist.length);
try
{
String mediaFile = filelist[randomInt];
descriptor = getAssets().openFd(mediaFile);
player.setDataSource(descriptor.getFileDescriptor(),descriptor.getStartOffset(),descriptor.getLength());
descriptor.close();
player.prepare();
player.start();
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
我的布局: -
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/background"
android:contentDescription="@string/desc" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="154dp"
android:layout_marginRight="92dp"
android:text="@string/button1" />
</RelativeLayout>
这是logcat: -
12-14 19:36:56.062: D/AndroidRuntime(540): Shutting down VM
12-14 19:36:56.062: W/dalvikvm(540): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
12-14 19:36:56.072: E/AndroidRuntime(540): FATAL EXCEPTION: main
12-14 19:36:56.072: E/AndroidRuntime(540): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mbawardi.layan/com.mbawardi.layan.MainActivity}: java.lang.NullPointerException
12-14 19:36:56.072: E/AndroidRuntime(540): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-14 19:36:56.072: E/AndroidRuntime(540): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-14 19:36:56.072: E/AndroidRuntime(540): at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-14 19:36:56.072: E/AndroidRuntime(540): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-14 19:36:56.072: E/AndroidRuntime(540): at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 19:36:56.072: E/AndroidRuntime(540): at android.os.Looper.loop(Looper.java:137)
12-14 19:36:56.072: E/AndroidRuntime(540): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-14 19:36:56.072: E/AndroidRuntime(540): at java.lang.reflect.Method.invokeNative(Native Method)
12-14 19:36:56.072: E/AndroidRuntime(540): at java.lang.reflect.Method.invoke(Method.java:511)
12-14 19:36:56.072: E/AndroidRuntime(540): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-14 19:36:56.072: E/AndroidRuntime(540): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-14 19:36:56.072: E/AndroidRuntime(540): at dalvik.system.NativeStart.main(Native Method)
12-14 19:36:56.072: E/AndroidRuntime(540): Caused by: java.lang.NullPointerException
12-14 19:36:56.072: E/AndroidRuntime(540): at com.mbawardi.layan.MainActivity.onCreate(MainActivity.java:39)
12-14 19:36:56.072: E/AndroidRuntime(540): at android.app.Activity.performCreate(Activity.java:4465)
12-14 19:36:56.072: E/AndroidRuntime(540): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-14 19:36:56.072: E/AndroidRuntime(540): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
12-14 19:36:56.072: E/AndroidRuntime(540): ... 11 more
12-14 19:36:59.042: I/Process(540): Sending signal. PID: 540 SIG: 9
12-14 19:37:06.032: D/AndroidRuntime(556): Shutting down VM
12-14 19:37:06.032: W/dalvikvm(556): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
12-14 19:37:06.042: E/AndroidRuntime(556): FATAL EXCEPTION: main
12-14 19:37:06.042: E/AndroidRuntime(556): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mbawardi.layan/com.mbawardi.layan.MainActivity}: java.lang.NullPointerException
12-14 19:37:06.042: E/AndroidRuntime(556): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-14 19:37:06.042: E/AndroidRuntime(556): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-14 19:37:06.042: E/AndroidRuntime(556): at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-14 19:37:06.042: E/AndroidRuntime(556): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-14 19:37:06.042: E/AndroidRuntime(556): at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 19:37:06.042: E/AndroidRuntime(556): at android.os.Looper.loop(Looper.java:137)
12-14 19:37:06.042: E/AndroidRuntime(556): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-14 19:37:06.042: E/AndroidRuntime(556): at java.lang.reflect.Method.invokeNative(Native Method)
12-14 19:37:06.042: E/AndroidRuntime(556): at java.lang.reflect.Method.invoke(Method.java:511)
12-14 19:37:06.042: E/AndroidRuntime(556): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-14 19:37:06.042: E/AndroidRuntime(556): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-14 19:37:06.042: E/AndroidRuntime(556): at dalvik.system.NativeStart.main(Native Method)
12-14 19:37:06.042: E/AndroidRuntime(556): Caused by: java.lang.NullPointerException
12-14 19:37:06.042: E/AndroidRuntime(556): at com.mbawardi.layan.MainActivity.onCreate(MainActivity.java:39)
12-14 19:37:06.042: E/AndroidRuntime(556): at android.app.Activity.performCreate(Activity.java:4465)
12-14 19:37:06.042: E/AndroidRuntime(556): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-14 19:37:06.042: E/AndroidRuntime(556): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
12-14 19:37:06.042: E/AndroidRuntime(556): ... 11 more
应用ρяσѕρєяK的解决方案后的第二个logcat: -
12-14 20:19:02.227: D/AndroidRuntime(537): Shutting down VM
12-14 20:19:02.237: W/dalvikvm(537): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
12-14 20:19:02.247: E/AndroidRuntime(537): FATAL EXCEPTION: main
12-14 20:19:02.247: E/AndroidRuntime(537): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mbawardi.layan/com.mbawardi.layan.MainActivity}: java.lang.NullPointerException
12-14 20:19:02.247: E/AndroidRuntime(537): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-14 20:19:02.247: E/AndroidRuntime(537): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-14 20:19:02.247: E/AndroidRuntime(537): at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-14 20:19:02.247: E/AndroidRuntime(537): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-14 20:19:02.247: E/AndroidRuntime(537): at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 20:19:02.247: E/AndroidRuntime(537): at android.os.Looper.loop(Looper.java:137)
12-14 20:19:02.247: E/AndroidRuntime(537): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-14 20:19:02.247: E/AndroidRuntime(537): at java.lang.reflect.Method.invokeNative(Native Method)
12-14 20:19:02.247: E/AndroidRuntime(537): at java.lang.reflect.Method.invoke(Method.java:511)
12-14 20:19:02.247: E/AndroidRuntime(537): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-14 20:19:02.247: E/AndroidRuntime(537): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-14 20:19:02.247: E/AndroidRuntime(537): at dalvik.system.NativeStart.main(Native Method)
12-14 20:19:02.247: E/AndroidRuntime(537): Caused by: java.lang.NullPointerException
12-14 20:19:02.247: E/AndroidRuntime(537): at com.mbawardi.layan.MainActivity.onCreate(MainActivity.java:42)
12-14 20:19:02.247: E/AndroidRuntime(537): at android.app.Activity.performCreate(Activity.java:4465)
12-14 20:19:02.247: E/AndroidRuntime(537): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-14 20:19:02.247: E/AndroidRuntime(537): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
12-14 20:19:02.247: E/AndroidRuntime(537): ... 11 more
答案 0 :(得分:0)
从导入中删除import android.R;
并导入您的项目R文件。或使用
descriptor = MainActivity.this.getAssets().openFd(mediaFile);
而不是
descriptor = MainActivity.this.getAssets().openFd("audioogg/"+mediaFile);