I am developing an android application for both tablet and phone using fragments. I was following the tutorial http://www.vogella.com/articles/Android/article.html#fragments_tutorial and created my own base adapter class. The basic idea of the implementation is the mainactivity must produce a gridview and when click on the image on the gridview it must open a new activity on phone and on tablet we must simultaneously view the two activites.
But somehow I am not able to run the application. It is being crashed while I start, leaving runtime errors. I am new to android development working on this problem from several weeks. Please help me.Thanks in advance.
Please find below my classes in the application
GridFragment.class
public class GridFragment extends Fragment{
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.gridview,container,false);
GridView gridView = (GridView) view.findViewById(R.id.gridView);
gridView.setAdapter(new EveryoneAdapter(view.getContext()));
// uses the view to get the context instead of getActivity().
return view;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
EveryoneAdapter.class
public class EveryoneAdapter extends BaseAdapter {
private Context mContext;
public EveryoneAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(final int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("onClick","position ["+position+"]");
}
});
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
}
DetailFragment.class
public class DetailFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details, container, false);
return view;
}
}
DetailActivity.class
public class DetailActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Need to check if Activity has been switched to landscape mode
// If yes, finished and go back to the start Activity
if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE) {
finish();
return;
}
setContentView(R.layout.details_activity_layout);
}
}
And my MainActivity.class
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Layout files are as follows
main.xml(Landscape mode)
<?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="horizontal" >
<fragment
android:id="@+id/gridFragment"
android:layout_width="150dip"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
class="com.fxpal.unity.android.GridFragment" ></fragment>
<fragment
android:id="@+id/detailFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.fxpal.unity.android.DetailFragment" >
<!-- Preview: layout=@layout/details -->
</fragment>
details.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="horizontal" >
<fragment
android:id="@+id/gridFragment"
android:layout_width="150dip"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
class="com.fxpal.unity.android.GridFragment" ></fragment>
<fragment
android:id="@+id/detailFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.fxpal.unity.android.DetailFragment" >
<!-- Preview: layout=@layout/details -->
</fragment>
details_activity_layout.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="horizontal" >
<fragment
android:id="@+id/gridFragment"
android:layout_width="150dip"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
class="com.fxpal.unity.android.GridFragment" ></fragment>
<fragment
android:id="@+id/detailFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.fxpal.unity.android.DetailFragment" >
<!-- Preview: layout=@layout/details -->
</fragment>
</LinearLayout>
main.xml(portrait mode)
<?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="horizontal" >
<fragment
android:id="@+id/gridFragment"
android:layout_width="150dip"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
class="com.fxpal.unity.android.GridFragment" ></fragment>
<fragment
android:id="@+id/detailFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.fxpal.unity.android.DetailFragment" >
<!-- Preview: layout=@layout/details -->
</fragment>
</LinearLayout>
And my Manifest.xml is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fxpal.unity.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="List of Mobiles"
android:name=".MainActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DetailActivity"
android:label="@string/app_name" >
</activity>
</application>
My error log is below
07-10 04:06:06.625: E/AndroidRuntime(1393): FATAL EXCEPTION: main
07-10 04:06:06.625: E/AndroidRuntime(1393): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fxpal.unity.android/com.fxpal.unity.android.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
07-10 04:06:06.625: E/AndroidRuntime(1393): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-10 04:06:06.625: E/AndroidRuntime(1393): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-10 04:06:06.625: E/AndroidRuntime(1393): at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-10 04:06:06.625: E/AndroidRuntime(1393): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-10 04:06:06.625: E/AndroidRuntime(1393): at android.os.Handler.dispatchMessage(Handler.java:99)
07-10 04:06:06.625: E/AndroidRuntime(1393): at android.os.Looper.loop(Looper.java:137)
07-10 04:06:06.625: E/AndroidRuntime(1393): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-10 04:06:06.625: E/AndroidRuntime(1393): at java.lang.reflect.Method.invokeNative(Native Method)
07-10 04:06:06.625: E/AndroidRuntime(1393): at java.lang.reflect.Method.invoke(Method.java:511)
07-10 04:06:06.625: E/AndroidRuntime(1393): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-10 04:06:06.625: E/AndroidRuntime(1393): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-10 04:06:06.625: E/AndroidRuntime(1393): at dalvik.system.NativeStart.main(Native Method)
07-10 04:06:06.625: E/AndroidRuntime(1393): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
07-10 04:06:06.625: E/AndroidRuntime(1393): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
07-10 04:06:06.625: E/AndroidRuntime(1393): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
07-10 04:06:06.625: E/AndroidRuntime(1393): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-10 04:06:06.625: E/AndroidRuntime(1393): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-10 04:06:06.625: E/AndroidRuntime(1393): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-10 04:06:06.625: E/AndroidRuntime(1393): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
07-10 04:06:06.625: E/AndroidRuntime(1393): at android.app.Activity.setContentView(Activity.java:1835)
07-10 04:06:06.625: E/AndroidRuntime(1393): at com.fxpal.unity.android.MainActivity.onCreate(MainActivity.java:14)
07-10 04:06:06.625: E/AndroidRuntime(1393): at android.app.Activity.performCreate(Activity.java:4465)
07-10 04:06:06.625: E/AndroidRuntime(1393): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-10 04:06:06.625: E/AndroidRuntime(1393): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-10 04:06:06.625: E/AndroidRuntime(1393): ... 11 more
07-10 04:06:06.625: E/AndroidRuntime(1393): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.fxpal.unity.android.ListFragment: make sure class name exists, is public, and has an empty constructor that is public
07-10 04:06:06.625: E/AndroidRuntime(1393): at android.app.Fragment.instantiate(Fragment.java:581)
07-10 04:06:06.625: E/AndroidRuntime(1393): at android.app.Fragment.instantiate(Fragment.java:549)
07-10 04:06:06.625: E/AndroidRuntime(1393): at android.app.Activity.onCreateView(Activity.java:4235)
07-10 04:06:06.625: E/AndroidRuntime(1393): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
07-10 04:06:06.625: E/AndroidRuntime(1393): ... 21 more
07-10 04:06:06.625: E/AndroidRuntime(1393): Caused by: java.lang.ClassNotFoundException: com.fxpal.unity.android.ListFragment
07-10 04:06:06.625: E/AndroidRuntime(1393): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
07-10 04:06:06.625: E/AndroidRuntime(1393): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
07-10 04:06:06.625: E/AndroidRuntime(1393): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
07-10 04:06:06.625: E/AndroidRuntime(1393): at android.app.Fragment.instantiate(Fragment.java:571)
07-10 04:06:06.625: E/AndroidRuntime(1393): ... 24 more
Either you haven’t copy-pasted all the layout files you’re using, or you’re loading the wrong layout file somewhere. The stacktrace clearly mentions that at some point it tries to load the fragment with fully qualified class name
com.fxpal.unity.android.ListFragmentfrom a layout file. However, There isn’t such aListFragmentclass in the snippets you provided.Do a search in your project for
com.fxpal.unity.android.ListFragmentand correct any references.