我想从此链接(http://jigsaweb.somee.com/Web/text.txt)逐行读取文本文件,并将每一行设置为一个String。之后,从每个字符串读取链接,然后连续播放视频。当我手动编写指向字符串的链接时,它终于可以工作了。但是,当我想从文本文件中读取链接时,我的应用程序会停止运行。在日志中显示“ java.lang.NullPointerException”。请帮我。我的代码有什么问题?
public class MainActivity extends AppCompatActivity {
VideoView videoView;
int i = 0;
int k = 1;
String path;
String link;
String link1;
String link2;
String link3;
List<String> videoPathes = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
videoView = (VideoView) findViewById(R.id.videoView);
try {
URL url = new URL("http://jigsaweb.somee.com/Web/text.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str = in.readLine();
while (str != null) {
switch (k) {
case 1:
link = str;
break;
case 2:
link1 = str;
break;
case 3:
link2 = str;
break;
case 4:
link3 = str;
break;
default:
link = null;
break;
}
k++;
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}catch (NetworkOnMainThreadException e){
}
try {
FileCache cache = new FileCache(new File(getExternalCacheDir(), "cachename"));
HttpUrlSource source = new HttpUrlSource(link);
HttpProxyCache proxyCache = new HttpProxyCache(source, cache);
videoPathes.add(proxyCache.getUrl());
FileCache cache1 = new FileCache(new File(getExternalCacheDir(), "cachename1"));
HttpUrlSource source1 = new HttpUrlSource(link1);
HttpProxyCache proxyCache1 = new HttpProxyCache(source1, cache1);
videoPathes.add(proxyCache1.getUrl());
FileCache cache2 = new FileCache(new File(getExternalCacheDir(), "cachename2"));
HttpUrlSource source2 = new HttpUrlSource(link2);
HttpProxyCache proxyCache2 = new HttpProxyCache(source2, cache2);
videoPathes.add(proxyCache2.getUrl());
FileCache cache3 = new FileCache(new File(getExternalCacheDir(), "cachename3"));
HttpUrlSource source3 = new HttpUrlSource(link3);
HttpProxyCache proxyCache3 = new HttpProxyCache(source3, cache3);
videoPathes.add(proxyCache3.getUrl());
} catch (ProxyCacheException e) {
Log.e("Tag1", "Error playing video", e);
}
path = "https://view.vzaar.com/15313699/video";
final Uri uri = Uri.parse(link);
videoView.setVideoPath(videoPathes.get(0));
videoView.requestFocus();
videoView.start();
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(final MediaPlayer mp) {
i = (i + 1) % videoPathes.size();
Log.d("tag0", i + "");
videoView.setVideoURI(Uri.parse(videoPathes.get(i)));
videoView.requestFocus();
videoView.start();
}
});
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
}
这是崩溃:
2019-01-13 16:08:59.870 7273-7273/com.karhakobyan.adplayer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.karhakobyan.adplayer, PID: 7273
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.karhakobyan.adplayer/com.karhakobyan.adplayer.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException
at com.danikula.videocache.Preconditions.checkNotNull(Preconditions.java:7)
at com.danikula.videocache.HttpUrlSource.<init>(HttpUrlSource.java:33)
at com.danikula.videocache.HttpUrlSource.<init>(HttpUrlSource.java:29)
at com.karhakobyan.adplayer.MainActivity.onCreate(MainActivity.java:108)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)