Android - file with opening error: no such file or directory

I get a strange list of errors, some seemingly turned off, but I have no way to say, because I can’t even download the debugging application. This is a very simple application, 3 radio buttons in a radio group, and each of them launches a different song for playback.

I'll start by publishing my manifest, my activity file and layout, then publish the error log.

manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cis298.lab2" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:label = "@string/app_name" android:name = ".LayoutActivity"> <intent-filter > <action android:name = "android.intent.action.MAIN" /> <category android:name = "android.intent.category.LAUNCHER" /> </intent-filter> ></activity> </application> </manifest> 

LayoutActivity:

 package com.cis298.lab2; import java.io.IOException; import com.cis298.lab2.R; import android.app.Activity; import android.os.Bundle; import android.media.MediaPlayer; import android.widget.RadioGroup; public class LayoutActivity extends Activity { int song; MediaPlayer mediaplayer = MediaPlayer.create(this, song); public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.layout); try { mediaplayer.prepare(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } RadioGroup radgrp = (RadioGroup) findViewById(R.id.radiogroup); radgrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId){ case R.id.dance: song = R.raw.redalert; mediaplayer.start(); break; case R.id.rap: song = R.raw.cannedheat; break; case R.id.rock: song = R.raw.movmou8105; break;} } }); } } 

location:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <RadioGroup android:id="@+id/radiogroup" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:orientation="vertical" > <RadioButton android:id="@+id/rock" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Play Rock" /> <RadioButton android:id="@+id/rap" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Play Rap" android:checked="true"/> <RadioButton android:id="@+id/dance" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Play Dance" android:textAlignment="center" /> </RadioGroup> </RelativeLayout> 

Log:

02-21 20: 34: 05.044: D / Android Runtime (322): shutdown VM 02-21 20: 34: 05.044: W / dalvikvm (322): threadid = 1: thread exiting with an uncaught exception (group = 0x4001d800) 02 -21 20: 34: 05.094: E / AndroidRuntime (322): FATAL EXCEPTION: main 02-21 20: 34: 05.094: E / AndroidRuntime (322): java.lang.RuntimeException: unable to instantiate activity ComponentInfo {com.cis298 .lab2 / com.cis298.lab2.LayoutActivity}: java.lang.NullPointerException 02-21 20: 34: 05.094: E / AndroidRuntime (322): with android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2585) 02- 21 20: 34: 05.094: E / AndroidRuntime (322): with android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2679) 02-21 20: 34: 05.094: E / AndroidRuntime (322): with android.app. ActivityThread.access $ 2300 (ActivityThread.java:125) 02-21 20: 34: 05.094: E / AndroidRuntime (322): with android. app.ActivityThread $ H.handleMessage (ActivityThread.java:2033) 02-21 20: 34: 05.094: E / AndroidRuntime (322): with android.os.Handler.dispatchMessage (Handler.java:99) 02-21 20: 34: 05.094: E / AndroidRuntime (322): with android.os.Looper.loop (Looper.java:123) 02-21 20: 34: 05.094: E / AndroidRuntime (322): with android.app.ActivityThread.main (ActivityThread.java:4627) 02-21 20: 34: 05.094: E / AndroidRuntime (322): with java.lang.reflect.Method.invokeNative (native method) 02-21 20: 34: 05.094: E / AndroidRuntime ( 322): when java.lang.reflect.Method.invoke (Method.javaβˆ—21) 02-21 20: 34: 05.094: E / AndroidRuntime (322): when com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:868) 02-21 20: 34: 05.094: E / AndroidRuntime (322): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:626) 02-21 20: 34: 05.094 : E / AndroidRuntime (322): with dalvik.system.NativeStart.main (native method) 02-21 20: 34: 05.094: E / AndroidRuntime (322): calling ano: java.lang.NullPointerException 02-21 20: 34: 05.094: E / AndroidRuntime (322): when android.content.ContextWrapper.getResources (ContextWrapper.java:80) 02-21 20: 34: 05.094: E / AndroidRuntime (322): when android.media.MediaPlayer.create (MediaPlayer.java:641) 02-21 20: 34: 05.094: E / AndroidRuntime (322): when com.cis298.lab2.LayoutActivity. (LayoutActivity.java:15) 02-21 20: 34: 05.094: E / AndroidRuntime (322): with java.lang.Class.newInstanceImpl (native method) 02-21 20: 34: 05.094: E / AndroidRuntime (322) : when java.lang.Class.newInstance (Class.java:1429) 02-21 20: 34: 05.094: E / AndroidRuntime (322): when android.app.Instrumentation.newActivity (Instrumentation.java:1021) 02-21 20: 34: 05.094: E / AndroidRuntime (322): with android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2577) 02-21 20: 34: 05.094: E / AndroidRuntime (322): ... 11 more

Any help would be greatly appreciated.

+6
source share
1 answer

Try moving mediaplayer = MediaPlayer.create(this, song); inside onCreate() .

Since activity is not fully initialized before onCreate() , this may be the cause of NPE.

However , the next problem is that you are creating a MediaPlayer with initially originally a song of 0 (int has a default value of 0 when it is an instance variable). Therefore, if moving MediaPlayer.create solves this NPE, the next error will be due to the inability to find the resource. So call MediaPlayer.create once song for something useful .

Perhaps something like this will work.

 public class LayoutActivity extends Activity { int song; MediaPlayer mediaPlayer; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.layout); RadioGroup radgrp = (RadioGroup) findViewById(R.id.radiogroup); radgrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId){ case R.id.dance: song = R.raw.redalert; break; case R.id.rap: song = R.raw.cannedheat; break; case R.id.rock: song = R.raw.movmou8105; break; } mediaplayer = MediaPlayer.create(LayoutActivity.this, song); try { mediaplayer.prepare(); mediaplayer.start(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } } 

And if the song is not used anywhere, you can save it in the onCheckedChanged() .

+3
source

All Articles