Issue
I have this in my fragment 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>
and here is my onCreate method:
@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();
}
}
Everything seems fine to me, but when I run the application it crashes with a lot of errors.
Here is the log:
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
What am I missing here? I know that it is something really small, but I'm not able to find it.
Solution
Try this..
Do your WebView initialization and others codes inside PlaceholderFragment.java. Because WebView is in fragment.xml
Or
Change setContentView(R.layout.activity_main); to setContentView(R.layout.fragment); and remove
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Answered By - Hariharan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.