【发布时间】:2011-05-03 21:39:51
【问题描述】:
我刚开始搞乱 android dev,我试图简单地播放网络上某处的视频文件。我的 Main.xml 看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:layout_height="wrap_content" android:id="@+id/button1" android:layout_width="wrap_content" android:text="@string/buttonText" android:onClick="clickHandler"></Button>
<VideoView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/MyVideoView"></VideoView>
</LinearLayout>
我的 java 文件如下所示: 包 com.dop.videoTest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class VideoTest extends Activity {
private String path = "http://commonsware.com/misc/test2.3gp";
private VideoView mVideoView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void clickHandler(View view)
{
mVideoView = (VideoView) findViewById(R.id.MyVideoView);
if (path == "") {
Toast.makeText(
VideoTest.this,
"Please edit VideoViewDemo Activity, and set path"
+ " variable to your media file URL/path",
Toast.LENGTH_LONG).show();
} else {
mVideoView.setVideoPath(path);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
}
}
}
所以当我点击按钮时,它只会显示“应用程序视频测试已意外停止。请重试。”
有什么想法吗?
【问题讨论】:
-
比较字符串使用equals(
path.equals("")),而不是path == "" -
做 == 工作正常。除此之外不会解决问题