Error inflating class fragment. Must specify unique android:id, android:tag, or have a parent with an id -
i trying run fragment application keeps on crashing when run it. trying load main activity can display webview in having no luck.
mainactivity
import android.os.bundle; import android.support.v4.app.fragmentactivity; public class mainactivity extends fragmentactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } }
fragment code
import android.app.fragment; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.webkit.websettings; import android.webkit.webview; public class webviewfragment extends fragment { private webview mwebview; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_main, container, false); mwebview = (webview) view.findviewbyid(r.id.webview); websettings websettings = mwebview.getsettings(); websettings.setjavascriptenabled(true); mwebview.loadurl("https://www.google.co.uk/"); return view; } }
manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.bashirsentongo.webviewapp"> <uses-permission android:name="android.permission.internet"/> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsrtl="true" android:theme="@style/apptheme"> <activity android:name=".mainactivity"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.bashirsentongo.webviewapp.mainactivity"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello world!" /> <fragment android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" class="com.example.bashirsentongo.webviewapp.webviewfragment"/> </relativelayout>
fragment_main.xml
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="layout.main"> <textview android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/hello_blank_fragment" /> <webview android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/webview"/> /> </framelayout>
from official documentation:
note: each fragment requires unique identifier system can use restore fragment if activity restarted (and can use capture fragment perform transactions, such remove it). there 3 ways provide id fragment: supply android:id attribute unique id. supply android:tag attribute unique string. if provide neither of previous two, system uses id of container view.
https://developer.android.com/guide/components/fragments.html
also find example here using id fragment:
<fragment android:name="com.example.news.articlelistfragment" android:id="@+id/list" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" />
Comments
Post a Comment