我正在尝试播放设备视频播放器的视频,使用Url视频和内部视频播放,但问题是当我播放任何视频时,它播放成功但白色返回应用程序屏幕时,应用程序崩溃。
在我的应用程序中,我使用带有viewpager的tabhost来交换选项卡,它有(tabhost)四个带有片段的选项卡。
Tabhost代码
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
TabInfo tabInfo = null;
MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("Tab1"),
(tabInfo = new TabInfo("Tab1", Tab1.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab2").setIndicator("Tab2"),
(tabInfo = new TabInfo("Tab2", Tab2.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab3").setIndicator("Tab3"),
(tabInfo = new TabInfo("Tab3", Tab3.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab4").setIndicator("Tab4"),
(tabInfo = new TabInfo("Tab4", Tab4.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
Viewpager代码
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, Tab1.class.getName()));
fragments.add(Fragment.instantiate(this, Tab2.class.getName()));
fragments.add(Fragment.instantiate(this, Tab3.class.getName()));
fragments.add(Fragment.instantiate(this, Tab4.class.getName()));
this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments);
//
this.mViewPager = (ViewPager) super.findViewById(R.id.viewpager);
this.mViewPager.setAdapter(this.mPagerAdapter);
this.mViewPager.setOnPageChangeListener(this);
this.mPagerAdapter.notifyDataSetChanged();
现在我正在播放来自自定义列表视图数据的特定网址视频。当tab2播放该视频时,
视频播放代码
Uri uri = Uri.parse(video_url.get(position));
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "video/mp4");
context.startActivity(intent);
最后我在下面代码的特定行上收到错误,
this.initialiseTabHost(savedInstanceState);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("Tab2"));
}
// Intialise ViewPager
this.intialiseViewPager();
}
protected void onSaveInstanceState(Bundle outState) {
outState.putString("Tab2", mTabHost.getCurrentTabTag());
super.onSaveInstanceState(outState);
}
OnTabChange Lister In detect Problem
TabInfo newTab = this.mapTabInfo.get(tag);
int pos = this.mTabHost.getCurrentTab();
this.mViewPager.setCurrentItem(pos);
this.mViewPager.setOffscreenPageLimit(4);
以下崩溃日志
10-15 13:27:33.612: E/AndroidRuntime(21790): FATAL EXCEPTION: main
10-15 13:27:33.612: E/AndroidRuntime(21790): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.MainActivity}: java.lang.NullPointerException
10-15 13:27:33.612: E/AndroidRuntime(21790): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
10-15 13:27:33.612: E/AndroidRuntime(21790): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
10-15 13:27:33.612: E/AndroidRuntime(21790): at android.app.ActivityThread.access$700(ActivityThread.java:143)
10-15 13:27:33.612: E/AndroidRuntime(21790): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
10-15 13:27:33.612: E/AndroidRuntime(21790): at android.os.Handler.dispatchMessage(Handler.java:99)
10-15 13:27:33.612: E/AndroidRuntime(21790): at android.os.Looper.loop(Looper.java:137)
10-15 13:27:33.612: E/AndroidRuntime(21790): at android.app.ActivityThread.main(ActivityThread.java:4960)
10-15 13:27:33.612: E/AndroidRuntime(21790): at java.lang.reflect.Method.invokeNative(Native Method)
10-15 13:27:33.612: E/AndroidRuntime(21790): at java.lang.reflect.Method.invoke(Method.java:511)
10-15 13:27:33.612: E/AndroidRuntime(21790): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
10-15 13:27:33.612: E/AndroidRuntime(21790): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
10-15 13:27:33.612: E/AndroidRuntime(21790): at dalvik.system.NativeStart.main(Native Method)
10-15 13:27:33.612: E/AndroidRuntime(21790): Caused by: java.lang.NullPointerException
10-15 13:27:33.612: E/AndroidRuntime(21790): at com.myapp.MainActivity.onTabChanged(MainActivity.java:354)
10-15 13:27:33.612: E/AndroidRuntime(21790): at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:401)
10-15 13:27:33.612: E/AndroidRuntime(21790): at android.widget.TabHost.setCurrentTab(TabHost.java:386)
10-15 13:27:33.612: E/AndroidRuntime(21790): at android.widget.TabHost.setCurrentTabByTag(TabHost.java:293)
10-15 13:27:33.612: E/AndroidRuntime(21790): at com.myapp.MainActivity.onCreate(MainActivity.java:217)
10-15 13:27:33.612: E/AndroidRuntime(21790): at android.app.Activity.performCreate(Activity.java:5203)
10-15 13:27:33.612: E/AndroidRuntime(21790): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
10-15 13:27:33.612: E/AndroidRuntime(21790): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
10-15 13:27:33.612: E/AndroidRuntime(21790): ... 11 more
答案 0 :(得分:0)
Finnaly我从群聊中获得解决方案,我必须更改以下代码,
this.initialiseTabHost(savedInstanceState);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("Tab2"));
}
// Intialise ViewPager
this.intialiseViewPager();
}
protected void onSaveInstanceState(Bundle outState) {
outState.putString("Tab2", mTabHost.getCurrentTabTag());
super.onSaveInstanceState(outState);
}
新代码
this.initialiseTabHost(savedInstanceState);
// Intialise ViewPager
this.intialiseViewPager();
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("Tab2"));
}
}
protected void onSaveInstanceState(Bundle outState) {
outState.putString("Tab2", mTabHost.getCurrentTabTag());
super.onSaveInstanceState(outState);
}