android:无法在标签视图中加载webview

时间:2014-03-29 10:08:08

标签: java android

我不知道为什么我的WebView无法在我的标签视图中加载。

当我在tab1中编写web.loadUrl("http://www.naver.com");的代码并执行程序时,应用程序停止。

以下是我的代码。你能帮助我吗?非常感谢提前! :)

tab1.java

public class Tab1 extends Fragment {
  Context mContext;

  public Tab1(Context context) {
   mContext = context;
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     View mainView = (View) inflater.inflate(R.layout.activity_main, container, false);
     WebView web = (WebView) mainView.findViewById(R.id.webview);
     web.loadUrl("http://www.naver.com"); <---this make program stop.   

       return mainView;



  }
}

main.java&lt; - 主要来源的一部分

public class SectionsPagerAdapter extends FragmentPagerAdapter {
  Context mContext;
  public SectionsPagerAdapter(android.support.v4.app.FragmentManager fm) {
   super(fm);
  }
  @Override
  public android.support.v4.app.Fragment getItem(int position) {
   // getItem is called to instantiate the fragment for the given page.
   // Return a DummySectionFragment (defined as a static inner class
   // below) with the page number as its lone argument.
   switch(position) {
   case 0:
    return new Tab1(mContext);
   case 1:
    return new Tab2(mContext);
   case 2:
    return new Tab3(mContext);
   case 3:
    return new Tab4(mContext);
   case 4:
    return new Tab5(mContext);
   }
   return null;
  }

activity_tab1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Tab1" >
           <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingBottom="10dip"
                android:paddingTop="15dip" >
                <WebView

                    android:id="@+id/webview"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dip"
                    android:layout_marginRight="10dip"
                    android:focusable="false"/>
            </LinearLayout>

</RelativeLayout>

AndroidManiFast.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="info.androidhive.slidingmenu"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="info.androidhive.slidingmenu.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        </application>
    <uses-permission android:name="android.permission.INTERNET" />



</manifest>

栈跟踪

03-29 20:08:22.588: D/dalvikvm(2575): Set heap target utilization to 768/1024 (0.750000)
03-29 20:08:22.588: D/ActivityThread(2575): setTargetHeapUtilization:0.75
03-29 20:08:22.588: D/ActivityThread(2575): setTargetHeapIdealFree:2097152
03-29 20:08:22.938: D/AbsListView(2575): Get MotionRecognitionManager
03-29 20:08:23.168: D/AbsListView(2575): Get MotionRecognitionManager
03-29 20:08:23.168: D/AndroidRuntime(2575): Shutting down VM
03-29 20:08:23.168: W/dalvikvm(2575): threadid=1: thread exiting with uncaught exception (group=0x41c3c360)
03-29 20:08:23.188: E/AndroidRuntime(2575): FATAL EXCEPTION: main
03-29 20:08:23.188: E/AndroidRuntime(2575): java.lang.NullPointerException
03-29 20:08:23.188: E/AndroidRuntime(2575):     at info.androidhive.slidingmenu.Tab1.onCreateView(Tab1.java:38)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.view.View.measure(View.java:15454)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:639)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.view.View.measure(View.java:15454)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5108)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.view.View.measure(View.java:15454)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.view.View.measure(View.java:15454)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5108)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2397)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.view.View.measure(View.java:15454)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1987)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1228)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1401)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1121)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4598)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.view.Choreographer.doFrame(Choreographer.java:525)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.os.Handler.handleCallback(Handler.java:615)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.os.Looper.loop(Looper.java:137)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at android.app.ActivityThread.main(ActivityThread.java:4950)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at java.lang.reflect.Method.invokeNative(Native Method)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at java.lang.reflect.Method.invoke(Method.java:511)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:997)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
03-29 20:08:23.188: E/AndroidRuntime(2575):     at dalvik.system.NativeStart.main(Native Method)
03-29 20:08:23.318: I/Process(2575): Sending signal. PID: 2575 SIG: 9
03-29 20:08:52.967: D/dalvikvm(2833): Set heap target utilization to 768/1024 (0.750000)
03-29 20:08:52.977: D/ActivityThread(2833): setTargetHeapUtilization:0.75
03-29 20:08:52.977: D/ActivityThread(2833): setTargetHeapIdealFree:2097152
03-29 20:08:53.308: D/AbsListView(2833): Get MotionRecognitionManager
03-29 20:08:53.418: D/AbsListView(2833): Get MotionRecognitionManager
03-29 20:08:53.418: D/AndroidRuntime(2833): Shutting down VM
03-29 20:08:53.418: W/dalvikvm(2833): threadid=1: thread exiting with uncaught exception (group=0x41c3c360)
03-29 20:08:53.438: E/AndroidRuntime(2833): FATAL EXCEPTION: main
03-29 20:08:53.438: E/AndroidRuntime(2833): java.lang.NullPointerException
03-29 20:08:53.438: E/AndroidRuntime(2833):     at info.androidhive.slidingmenu.Tab1.onCreateView(Tab1.java:38)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.view.View.measure(View.java:15454)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:639)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.view.View.measure(View.java:15454)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5108)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.view.View.measure(View.java:15454)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.view.View.measure(View.java:15454)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5108)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2397)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.view.View.measure(View.java:15454)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1987)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1228)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1401)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1121)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4598)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.view.Choreographer.doFrame(Choreographer.java:525)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.os.Handler.handleCallback(Handler.java:615)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.os.Looper.loop(Looper.java:137)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at android.app.ActivityThread.main(ActivityThread.java:4950)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at java.lang.reflect.Method.invokeNative(Native Method)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at java.lang.reflect.Method.invoke(Method.java:511)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:997)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
03-29 20:08:53.438: E/AndroidRuntime(2833):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

片段应该没有arg构造函数

public Tab1(Context context) {
mContext = context;
}

删除上述

并改变这个

return new Tab1(mContext);

return new Tab1();

与其他人相似。

如果您希望Context Framgent使用getActivity()

您还需要移动此

<uses-permission android:name="android.permission.INTERNET"/>

在清单文件中的application标记之前

编辑:

建议摆脱LinearLayout中的activity_tab1.xmlRelativeLayout就足够了。

同时使用match_parent代替fill_parent

编辑2:

更改此

View mainView = (View) inflater.inflate(R.layout.activity_main, container, false);

View mainView = (View) inflater.inflate(R.layout.activity_tab1, container, false);