这些是我的语法错误,我不知道怎么弄清楚,
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;
}
}
LOG猫,
05-18 17:37:14.272:D / AndroidRuntime(486):关闭VM
05-18 17:37:14.272:W / dalvikvm(486):threadid = 1:线程退出,未捕获异常(组= 0x40015560)
05-18 17:37:14.291:E / AndroidRuntime(486):致命异常:主要
05-18 17:37:14.291:E / AndroidRuntime(486):java.lang.NullPointerException
05-18 17:37:14.291:E / AndroidRuntime(486):at com.hiphop.streamingmediaplayer.Jsonmedia $ 4.onClick(Jsonmedia.java:114)
05-18 17:37:14.291:E / AndroidRuntime(486):在android.view.View.performClick(View.java:2485)
05-18 17:37:14.291:E / AndroidRuntime(486):在android.view.View $ PerformClick.run(View.java:9080)
05-18 17:37:14.291:E / AndroidRuntime(486):在android.os.Handler.handleCallback(Handler.java:587)
05-18 17:37:14.291:E / AndroidRuntime(486):在android.os.Handler.dispatchMessage(Handler.java:92)
05-18 17:37:14.291: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):at java.lang.reflect.Method.invokeNative(Native Method)
05-18 17:37:14.291:E / AndroidRuntime(486):at java.lang.reflect.Method.invoke(Method.java:507)
05-18 17:37:14.291:E / AndroidRuntime(486):at 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
答案 0 :(得分:0)
您在song_url
块内定义try
因此无法在外部使用。
所以在try
之前声明它们,如:
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);
int current_song = 0;
String song_url[] = null;
try {
....
....
song_url = new String[list.length];
}