0

I have an XML layout using weight layout like this.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2"
android:orientation="vertical" >

    <WebView
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
     />

    <android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
     />
</LinearLayout>

In my Java file, I do like this, then my app crashes. If I don't use the weight layout, everything works fine.

WebView wv = (WebView) findViewById(R.id.webView1);
double-beep
  • 5,031
  • 17
  • 33
  • 41
Emmy
  • 3,949
  • 5
  • 28
  • 30
  • does `my app crashes` mean that `findViewById()` returns `null` and and it crashes when you try to call methods on the null object? – neevek Mar 05 '13 at 04:38
  • Hi Pragnani and Nevvek: I cleaned the project and everything works. :) Thanks for your help anyway. – Emmy Mar 05 '13 at 04:59

3 Answers3

2

I think everything is fine. If you are using eclipse then Go to project -> clean and clean your project. then try to run or else restart your adb and eclipse. Then try to run.

Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
  • I did some testing and the app works if I switch the webview and viewpage position. like this. Please see the answer I posted above. I could not post here since the text is too long here. – Emmy Mar 05 '13 at 04:50
1

The app works if I switch the webview and viewpage position like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2"
android:orientation="vertical" >



<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    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="0dp"
            android:layout_weight="1"

     />
</LinearLayout>
Emmy
  • 3,949
  • 5
  • 28
  • 30
0

Try to set height for both of the controls as match_parent instead of 0dp

Syn3sthete
  • 4,151
  • 3
  • 23
  • 41