我的片段xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/sphereIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/rsz_rsz_mystic"
android:contentDescription="@string/magicBallDescr"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
</LinearLayout>
这是我的onCreate
方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webview;
webview = (WebView) findViewById(R.id.webView1);
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("http://www.google.com");
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
对我来说一切似乎都很好,但是当我运行应用程序时,它会因很多错误而崩溃。
这是日志:
04-26 07:27:22.818: E/AndroidRuntime(1834): FATAL EXCEPTION: main
04-26 07:27:22.818: E/AndroidRuntime(1834): Process: com.gelasoft.answeringball, PID: 1834
04-26 07:27:22.818: E/AndroidRuntime(1834): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gelasoft.answeringball/com.gelasoft.answeringball.MainActivity}: java.lang.NullPointerException
04-26 07:27:22.818: E/AndroidRuntime(1834): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-26 07:27:22.818: E/AndroidRuntime(1834): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-26 07:27:22.818: E/AndroidRuntime(1834): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-26 07:27:22.818: E/AndroidRuntime(1834): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-26 07:27:22.818: E/AndroidRuntime(1834): at android.os.Handler.dispatchMessage(Handler.java:102)
04-26 07:27:22.818: E/AndroidRuntime(1834): at android.os.Looper.loop(Looper.java:136)
04-26 07:27:22.818: E/AndroidRuntime(1834): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-26 07:27:22.818: E/AndroidRuntime(1834): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 07:27:22.818: E/AndroidRuntime(1834): at java.lang.reflect.Method.invoke(Method.java:515)
04-26 07:27:22.818: E/AndroidRuntime(1834): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-26 07:27:22.818: E/AndroidRuntime(1834): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-26 07:27:22.818: E/AndroidRuntime(1834): at dalvik.system.NativeStart.main(Native Method)
04-26 07:27:22.818: E/AndroidRuntime(1834): Caused by: java.lang.NullPointerException
04-26 07:27:22.818: E/AndroidRuntime(1834): at com.gelasoft.answeringball.MainActivity.onCreate(MainActivity.java:24)
04-26 07:27:22.818: E/AndroidRuntime(1834): at android.app.Activity.performCreate(Activity.java:5231)
04-26 07:27:22.818: E/AndroidRuntime(1834): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-26 07:27:22.818: E/AndroidRuntime(1834): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-26 07:27:22.818: E/AndroidRuntime(1834): ... 11 more
我在这里缺少什么?我知道它确实很小,但我找不到它。
答案 0 :(得分:2)
试试这个..
在WebView
内进行PlaceholderFragment.java
初始化和其他代码。因为WebView
位于fragment.xml
或者
将setContentView(R.layout.activity_main);
更改为setContentView(R.layout.fragment);
并删除
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
答案 1 :(得分:1)
布局XML用于活动中的Fragment,这就是findViewById()找不到您的视图并返回null的原因。
将代码移入PlaceHolderFragment内部类或将WebView xml元素移动到activity_main.xml。
当您使用eclipse时,它会在向导的每个新Activity创建时添加一个新的Fragment。我个人认为这是错误的,但我知道Android团队正试图让每个人都到处使用片段。