Reputation: 25
I'm working with Beginning Android 4 Application Development by Wei-Meng Lee and stuck in Chapter 2 where they introduce fragments. I have followed the demo code by the letter yet continue to have the application fail at runtime. Any workarounds I have found on the internet always produce the same error. Looking through the LogCat it appears the primary source is a java.lang.ClassNotFoundException and android.app.Fragment$InstantiationException
I have tried to use the the android.support.v4.app.Fragment as well as the android.support.v4.app.FragmentActivity and have my main activity extend it instead, but neither have provided a solution. I examined the build path and toyed around with it for a bit with no luck either. I have no idea why the program cannot find my classes as they are located in what I believe is the correct directory.
Here is the LogCat Error output:
04-20 14:10:35.572: E/AndroidRuntime(1114): FATAL EXCEPTION: main
04-20 14:10:35.572: E/AndroidRuntime(1114): Process: solaris.data.demofragment, PID: 1114
04-20 14:10:35.572: E/AndroidRuntime(1114): java.lang.RuntimeException: Unable to start activity ComponentInfo{solaris.data.demofragment/solaris.data.demofragment.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.os.Handler.dispatchMessage(Handler.java:102)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.os.Looper.loop(Looper.java:136)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-20 14:10:35.572: E/AndroidRuntime(1114): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 14:10:35.572: E/AndroidRuntime(1114): at java.lang.reflect.Method.invoke(Method.java:515)
04-20 14:10:35.572: E/AndroidRuntime(1114): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-20 14:10:35.572: E/AndroidRuntime(1114): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-20 14:10:35.572: E/AndroidRuntime(1114): at dalvik.system.NativeStart.main(Native Method)
04-20 14:10:35.572: E/AndroidRuntime(1114): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
04-20 14:10:35.572: E/AndroidRuntime(1114): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.Activity.setContentView(Activity.java:1929)
04-20 14:10:35.572: E/AndroidRuntime(1114): at solaris.data.demofragment.MainActivity.onCreate(MainActivity.java:19)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.Activity.performCreate(Activity.java:5231)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-20 14:10:35.572: E/AndroidRuntime(1114): ... 11 more
04-20 14:10:35.572: E/AndroidRuntime(1114): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment solaris.data.demofragment.fragment1: make sure class name exists, is public, and has an empty constructor that is public
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.Fragment.instantiate(Fragment.java:597)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.Fragment.instantiate(Fragment.java:561)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.Activity.onCreateView(Activity.java:4778)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
04-20 14:10:35.572: E/AndroidRuntime(1114): ... 21 more
04-20 14:10:35.572: E/AndroidRuntime(1114): Caused by: java.lang.ClassNotFoundException: Didn't find class "solaris.data.demofragment.fragment1" on path: DexPathList[[zip file "/data/app/solaris.data.demofragment-1.apk"],nativeLibraryDirectories=[/data/app-lib/solaris.data.demofragment-1, /system/lib]]
04-20 14:10:35.572: E/AndroidRuntime(1114): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
04-20 14:10:35.572: E/AndroidRuntime(1114): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
04-20 14:10:35.572: E/AndroidRuntime(1114): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
04-20 14:10:35.572: E/AndroidRuntime(1114): at android.app.Fragment.instantiate(Fragment.java:583)
04-20 14:10:35.572: E/AndroidRuntime(1114): ... 24 more
Main activity:
package solaris.data.demofragment;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:name="solaris.data.demofragment.fragment1"
android:id="@+id/fragment1"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" />
<fragment
android:name="solaris.data.demofragment.fragment2"
android:id="@+id/fragment2"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" />
</LinearLayout>
fragment1.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"
android:background="#00FF00"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is fragment #1"
android:textColor="#000000"
android:textSize="25sp" />
</LinearLayout>
Fragment1.java
package solaris.data.demofragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
}
Fragment2.java and fragment2.xml are identical to their respective 1 counterparts, except for name, the text displayed, and color. All .xml files are located in the res/layout folder, and all java files are located in the src/solaris.data.demofragment package. The min sdk-version is 14 with the target as 19.
Upvotes: 0
Views: 101
Reputation: 5122
The name
attribute for your fragments that you defined in activity_main.xml
are incorrect.
Instead of
android:name="solaris.data.demofragment.fragment1"
it should be
android:name="solaris.data.demofragment.Fragment1"
This is because they are case-sensitive as you defined it as
public class Fragment1 extends Fragment
So when android inflated your activity_main.xml
, it could not find a fragment with the name of fragment1
. This will also apply to your fragment2
if the class is Fragment2
.
Upvotes: 3