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.
If I use weight layout, then my Java file cannot find the view by id
-
18-03-2022 - |
Question
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);
Solution
OTHER TIPS
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>
Try to set height for both of the controls as match_parent
instead of 0dp
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow