我正在使用包含三个标签的FragmentTabHost。我在第一个选项卡的onCreateView中得到一个空指针异常,我正在尝试添加一个webViewClient ..我是android的新手,我无法弄清楚代码有什么问题......
我以前在我的主布局文件中有一个侧边栏/汉堡包抽屉,但是只要我从我的activity_lest_see_main xml文件中删除它..我开始在Tab1Activity中获取NullPointerException ..请帮助..我真的被困在这个问题。 只要我在我的lest_see_main.xml文件中添加侧边栏/汉堡包抽屉,代码就会开始运行而不会出现任何错误,并且应用程序正常运行。
我始终在NullPointerException
mwebView.setWebViewClient(new HelloWebViewClient)
处获得Tab1Activity
。
这是我在Logcat中得到的以及顶部的NullPointerException -
07-28 18:24:52.735: E/AndroidRuntime(6834): FATAL EXCEPTION: main
07-28 18:24:52.735: E/AndroidRuntime(6834): java.lang.NullPointerException
07-28 18:24:52.735: E/AndroidRuntime(6834): at com.example.itslive.Tab1Activity.onCreateView(Tab1Activity.java:168)
07-28 18:24:52.735: E/AndroidRuntime(6834): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
07-28 14:17:31.274: E/AndroidRuntime(29029): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
07-28 14:17:31.274: E/AndroidRuntime(29029): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
07-28 14:17:31.274: E/AndroidRuntime(29029): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
07-28 14:17:31.274: E/AndroidRuntime(29029): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
这是我的主要Activity的xml文件,它扩展了Fragment Activity
activity_lest_see_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rev"
android:layout_width="match_parent"
android:layout_height = "match_parent"
android:layout_gravity="start">
<RelativeLayout
android:id="@+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v4.app.FragmentTabHost
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabhost">
<LinearLayout
android:id="@+id/LinearLayout01"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</FrameLayout>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
<Button
android:id="@+id/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
android:text = "@string/exit_button"
android:textColor="@color/blue"
android:textSize="20sp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
</RelativeLayout>
这是扩展Fragmentactivity的MainActivity java文件
LetsSeeMainActivity.java
public class LestSeeMainActivity extends FragmentActivity {
private FragmentTabHost tabHost;
int flag = 0;
int n;
private static final String TAG = "LetsSeeMainActivity";
private AudioManager maudioManager;
Fragment mContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lest_see_main);
maudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
final Button exitb = (Button)findViewById(R.id.exit);
exitb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
maudioManager.playSoundEffect(AudioManager.FX_KEY_CLICK);
finish();
}
});
tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
tabHost.addTab(
tabHost.newTabSpec("tab1").setIndicator("Tab1", null),
Tab1Activity.class, null);
tabHost.addTab(
tabHost.newTabSpec("tab2").setIndicator("Tab2", null),
Tab2Activity.class, null);
tabHost.addTab(
tabHost.newTabSpec("tab3").setIndicator("Tab3", null),
Tab3Activity.class, null);
}
}
这是我的第一个Fragment Activity java文件的onCreateView
Tab1Activity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
if(v==null)
{
v = inflater.inflate(R.layout.activity_tab1, container, false);
mwebView = (WebView) v.findViewById(R.id.wb1);
mwebView.setWebViewClient(new HelloWebViewClient());
registerForContextMenu(mwebView);
}
这是我的activity_tab1.xml文件 -
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView android:id="@+id/wb1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxHeight="5dp"
android:minHeight="5dp"
android:visibility="invisible"
/>
</android.support.v4.widget.SwipeRefreshLayout>
答案 0 :(得分:0)
尝试放
mwebView = (WebView) v.findViewById(R.id.wb1);
mwebView.setWebViewClient(new HelloWebViewClient());
registerForContextMenu(mwebView);
onViewCreated(..)中的。