这些是我的语法错误,我不知道如何弄清楚,
current_song 无法解析为变量
此行有多个标记,song_url 无法解析为变量
我如何解决上述错误。我无法在我的方法中访问 song_url 和 current_song,例如,
next.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp.stop();
++current_song;
mp.setDataSource(song_url[current_song]);
mp.start();
}
});
我的完整代码,
public class Jsonmedia extends Activity {
private MediaPlayer mp;
Button play, pause, next;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jsonview);
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
play = (Button) findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
next = (Button) findViewById(R.id.next);
try {
// Create a URL for the desired page
URL url = new URL("http://reallifethug.webs.com/temp_list.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(
url.openStream()));
int ch, current_song = 0, EOF = -1; // end of file
String list[];
String str = "";
while ((ch = in.read()) != EOF)
str += (char) ch;
in.close();
list = str.split("\n");
for (ch = 0; ch < list.length; ++ch)
list[ch] += "\n";
String song_name[] = new String[list.length];
String song_url[] = new String[list.length];
for (int i = 0; i < list.length; i++) {
song_name[i] = "";
song_url[i] = "";
int a = 0;
char c;
while ((c = list[i].charAt(a++)) != ' ')
song_name[i] += c;
while ((c = list[i].charAt(a++)) != '\n')
song_url[i] += c;
}
mp.setDataSource(song_url[current_song]);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.prepareAsync();
play.setEnabled(false);
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
play.setEnabled(true);
}
});
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp.start();
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp.pause();
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp.stop();
++current_song;
mp.setDataSource(song_url[current_song]);
mp.start();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.jsonmedia, menu);
return true;
}
}
E/AndroidRuntime(486): 在 android.os.Looper.loop(Looper.java:123) 05-18 17:37:14.291: E/AndroidRuntime(486): 在 android.app.ActivityThread.main(ActivityThread.java :3683) 05-18 17:37:14.291: E/AndroidRuntime(486): 在 java.lang.reflect.Method.invokeNative(Native Method) 05-18 17:37:14.291: E/AndroidRuntime(486): 在java.lang.reflect.Method.invoke(Method.java:507) 05-18 17:37:14.291: E/AndroidRuntime(486): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java :839) 05-18 17:37:14.291: E/AndroidRuntime(486): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 05-18 17:37:14.291: E/AndroidRuntime (486): 在 dalvik.system.NativeStart.main(Native Method) 05-18 17:37:16.301: I/Process(486): 发送信号。PID:486 SIG:9 在 android.app.ActivityThread.main(ActivityThread.java:3683) 05-18 17:37:14.291: E/AndroidRuntime(486): 在 java.lang.reflect.Method.invokeNative(Native Method) 05-18 17: 37:14.291: E/AndroidRuntime(486): 在 java.lang.reflect.Method.invoke(Method.java:507) 05-18 17:37:14.291: E/AndroidRuntime(486): 在 com.android.internal .os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 05-18 17:37:14.291: E/AndroidRuntime(486): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 05-18 17:37:14.291: E/AndroidRuntime(486): at dalvik.system.NativeStart.main(Native Method) 05-18 17:37:16.301: I/Process(486): 发送信号。PID:486 SIG:9 在 android.app.ActivityThread.main(ActivityThread.java:3683) 05-18 17:37:14.291: E/AndroidRuntime(486): 在 java.lang.reflect.Method.invokeNative(Native Method) 05-18 17: 37:14.291: E/AndroidRuntime(486): 在 java.lang.reflect.Method.invoke(Method.java:507) 05-18 17:37:14.291: E/AndroidRuntime(486): 在 com.android.internal .os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 05-18 17:37:14.291: E/AndroidRuntime(486): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 05-18 17:37:14.291: E/AndroidRuntime(486): at dalvik.system.NativeStart.main(Native Method) 05-18 17:37:16.301: I/Process(486): 发送信号。PID:486 SIG:9 在 java.lang.reflect.Method.invoke(Method.java:507) 05-18 17:37:14.291: E/AndroidRuntime(486): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit. java:839) 05-18 17:37:14.291: E/AndroidRuntime(486): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 05-18 17:37:14.291: E/ AndroidRuntime(486): at dalvik.system.NativeStart.main(Native Method) 05-18 17:37:16.301: I/Process(486): 发送信号。PID:486 SIG:9 在 java.lang.reflect.Method.invoke(Method.java:507) 05-18 17:37:14.291: E/AndroidRuntime(486): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit. java:839) 05-18 17:37:14.291: E/AndroidRuntime(486): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 05-18 17:37:14.291: E/ AndroidRuntime(486): at dalvik.system.NativeStart.main(Native Method) 05-18 17:37:16.301: I/Process(486): 发送信号。PID:486 SIG:9 I/Process(486):发送信号。PID:486 SIG:9 I/Process(486):发送信号。PID:486 SIG:9