我正在使用包含三個選項卡的FragmentTabHost。我得到一個空指針異常在第一個選項卡,我試圖添加一個webViewClient的onCreateView ..我是新來的android和我無法弄清楚代碼有什麼問題...使用FragmentTabHost的onCreateView中的空指針異常
我曾經在我的主佈局文件中有一個側邊欄/漢堡包抽屜,但只要我從我的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_lest_see_main.xml
主要活動我的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>
這是我的MainActivity java文件擴展Fragmentactivity
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);
}
}
這是我的第一個片段活動的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>
您可以查看此[鏈接](http://androhub.com/android-swiperefresh-layout/)。 –
謝謝,但這沒有幫助..因爲我的主要和唯一的問題是這種nullpointerexception。 –
發佈您的完整Logcat –