I am getting a runtimeException with the following code, does anyone know why?
public class FragsApplicationActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
SuggestionFrags class
public class SuggestionFrags extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return super.onCreateView(inflater, container, savedInstanceState);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="com.samplefragsapplication.SuggestionFrags" />
</LinearLayout>
I am getting this exception:
11-23 18:09:23.899: E/AndroidRuntime(2436): FATAL EXCEPTION: main
11-23 18:09:23.899: E/AndroidRuntime(2436): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.samplefragsapplication/com.samplefragsapplication.FragsApplicationActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.os.Looper.loop(Looper.java:123)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-23 18:09:23.899: E/AndroidRuntime(2436): at java.lang.reflect.Method.invokeNative(Native Method)
11-23 18:09:23.899: E/AndroidRuntime(2436): at java.lang.reflect.Method.invoke(Method.java:521)
11-23 18:09:23.899: E/AndroidRuntime(2436): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-23 18:09:23.899: E/AndroidRuntime(2436): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-23 18:09:23.899: E/AndroidRuntime(2436): at dalvik.system.NativeStart.main(Native Method)
11-23 18:09:23.899: E/AndroidRuntime(2436): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-23 18:09:23.899: E/AndroidRuntime(2436): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.app.Activity.setContentView(Activity.java:1647)
11-23 18:09:23.899: E/AndroidRuntime(2436): at com.samplefragsapplication.FragsApplicationActivity.onCreate(FragsApplicationActivity.java:12)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-23 18:09:23.899: E/AndroidRuntime(2436): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.samplefragsapplication-2.apk]
11-23 18:09:23.899: E/AndroidRuntime(2436): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
11-23 18:09:23.899: E/AndroidRuntime(2436): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
11-23 18:09:23.899: E/AndroidRuntime(2436): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.view.LayoutInflater.createView(LayoutInflater.java:466)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
11-23 18:09:23.899: E/AndroidRuntime(2436): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
The inflate exception is not actually the problem but really comes from another deeper issue in your layout that is then wrapped in an InflateException . A common issue is an out of memory exception when trying to inflate an ImageView loading a drawable resource.
FragmentContainerView is a customized Layout designed specifically for Fragments. It extends FrameLayout , so it can reliably handle Fragment Transactions, and it also has additional features to coordinate with fragment behavior.
Another way to simply add a Fragment to FragmentContainerView is by using the attribute android:name=”fragment_class_name_path" in XML. Both the attributes android:name or class are similar things we just need to give the classpath as a value to inflate the fragment.
If you are going to use Fragments
you need to extend FragmentActivity
not Activity
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With