here i try to add menu in my app screen , and when i run the app , its crash and show me this error :
@layout/activity_main
does not contain a declaration with id webview
to understand my problem take a look to my code
activity layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:itemIconTint="@drawable/tab"
app:itemTextColor="@drawable/tab"
app:headerLayout="@layout/nav_header"
app:menu="@menu/menu" />
</androidx.drawerlayout.widget.DrawerLayout>
and this content layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<WebView
tools:context=".MainActivity"
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</WebView>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
</LinearLayout>
part of my code from main activity (because its so long) , the error show with this (R.id.webview)
public class MainActivity extends AppCompatActivity {
LinearLayout LinearLayoutView;
private WebView webView;
private AdView mAdView;
private InterstitialAd mInterstitialAd;
DrawerLayout drawer_layout;
NavigationView nav_view;
ActionBarDrawerToggle drawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer_layout = findViewById(R.id.drawer_layout);
nav_view = findViewById(R.id.nav_view);
drawerToggle = new ActionBarDrawerToggle(this,drawer_layout, R.string.open, R.string.close);
drawer_layout.addDrawerListener(drawerToggle);
drawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
nav_view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId())
{
case R.id.nav_item01:
{
Toast.makeText(MainActivity.this, "this nav item 1 ", Toast.LENGTH_LONG).show();
}
case R.id.nav_item02:
{
Toast.makeText(MainActivity.this, "this nav item 1 ", Toast.LENGTH_LONG).show();
}
case R.id.nav_item03:
{
Toast.makeText(MainActivity.this, "this nav item 1 ", Toast.LENGTH_LONG).show();
}
case R.id.nav_item04:
{
Toast.makeText(MainActivity.this, "this nav item 1 ", Toast.LENGTH_LONG).show();
}
case R.id.nav_item05:
{
Toast.makeText(MainActivity.this, "this nav item 1 ", Toast.LENGTH_LONG).show();
}
case R.id.nav_item06:
{
Toast.makeText(MainActivity.this, "this nav item 1 ", Toast.LENGTH_LONG).show();
}
case R.id.nav_item07:
{
Toast.makeText(MainActivity.this, "this nav item 1 ", Toast.LENGTH_LONG).show();
}
case R.id.nav_share:
{
ShareApp();
}
case R.id.rateapp:
{
RateApp();
}
case R.id.moreapps:
{
MoreApps();
}
}
return false;
}
});
WebView webView = findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webviewscreen();
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSaveFormData(false);
webSettings.setSupportZoom(false);
webSettings.setGeolocationEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setUseWideViewPort(true);
webSettings.setDomStorageEnabled(true);
webView.setHapticFeedbackEnabled(false);
errors :
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: app.example.webview, PID: 8114
java.lang.RuntimeException: Unable to start activity ComponentInfo{app.example.webview/app.example.webview.MainActivity}: android.view.InflateException: Binary XML file line #23: Binary XML file line #23: Error inflating class com.google.android.material.navigation.NavigationView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: android.view.InflateException: Binary XML file line #23: Binary XML file line #23: Error inflating class com.google.android.material.navigation.NavigationView
Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class com.google.android.material.navigation.NavigationView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:775)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:197)
at app.example.webview**strong text**.MainActivity.onCreate(MainActivity.java:58)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: android.content.res.Resources$NotFoundException: Can't find ColorStateList from drawable resource ID #0x7f0700f6
at android.content.res.ResourcesImpl.loadColorStateList(ResourcesImpl.java:1013)
at android.content.res.Resources.getColorStateList(Resources.java:1021)
at androidx.core.content.res.ResourcesCompat$Api23Impl.getColorStateList(ResourcesCompat.java:688)
at androidx.core.content.res.ResourcesCompat.getColorStateList(ResourcesCompat.java:244)
at androidx.core.content.ContextCompat.getColorStateList(ContextCompat.java:558)
at androidx.appcompat.content.res.AppCompatResources.getColorStateList(AppCompatResources.java:48)
at androidx.appcompat.widget.TintTypedArray.getColorStateList(TintTypedArray.java:179)
at com.google.android.material.navigation.NavigationView.<init>(NavigationView.java:213)
E/AndroidRuntime: at com.google.android.material.navigation.NavigationView.<init>(NavigationView.java:141)
26 more
i watch many videos and they do exactly what I do , and its work with them
3
Answers
In your
MainActivity
class, you are setting the view as:However, your layout file
activity_main.xml
does not contain aWebView
widget and hence you are getting that error.Please try moving/creating the
WebView
widget inside theactivity_main.xml
file in order to resolve the error.Please let me know in case you need anything else.
Hope that helps.
In your activity_main.xml file you have to add include tag like this
this include tag helps the MainActivity.java to find (R.id.webview) in the class and due to this include tag
android.view.InflateException
will resolve and your app run smoothly.From your log stacktrace, it indicates there is a problem with your
NavigationView
:And the main cause should be:
So there should be something wrong with your
@drawable/tab
:You should check if the value of
@drawable/tab
is properly defined.