3

This is my first post, so I apologize in advance...

I'm trying to compile a simple Drawer sample/practice app. When I click on the items in the listview, however, it crashes. Logcat says that it's not able to instantiate from android.app.Fragment. I've tried also using the android.support.v4 library -- assuming I implemented it correctly, which is dubious -- with the same results. Frankly I'm not sure what the difference is. I'm totally stumped. Please help!

MainActivity:

package pac.gazette;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

    private String[] fragmentNames;
    private String[] fragmentAddresses;

    private DrawerLayout drawer;
    private ListView drawerList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragmentNames = getResources().getStringArray(R.array.fragment_names);
        fragmentAddresses = getResources().getStringArray(
                R.array.fragment_options);
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawerList = (ListView) findViewById(R.id.left_drawer);
        drawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list, fragmentNames));
        drawerList.setOnItemClickListener (new CustomClickListener());

    }

    public class CustomClickListener implements OnItemClickListener {

        @SuppressLint("NewApi") @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // drawer close handling
            drawer.closeDrawer(drawerList);
            // title handling (does not wait for drawer close)
            if (position == 0) {
                setTitle(new String("East Asia Gazette"));
            } else {
                setTitle(fragmentNames[position]);
            }
            // new fragment handling
            FragmentTransaction transaction = getFragmentManager()
                    .beginTransaction();
            transaction.replace(R.id.content_frame, Fragment.instantiate(
                    MainActivity.this, fragmentAddresses[position]));
            transaction.commit();   
        }
    }

}

Xml:

<android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <!-- The main content view -->
    <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    <!-- The navigation drawer -->
    <ListView
            android:id="@+id/left_drawer"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="#940032"/>
</android.support.v4.widget.DrawerLayout>

Logcat:

08-25 12:25:02.579: E/AndroidRuntime(17137): FATAL EXCEPTION: main
08-25 12:25:02.579: E/AndroidRuntime(17137): android.app.Fragment$InstantiationException: Unable to instantiate fragment pac.gazette.fragment_feed.xml: make sure class name exists, is public, and has an empty constructor that is public
08-25 12:25:02.579: E/AndroidRuntime(17137):    at android.app.Fragment.instantiate(Fragment.java)
08-25 12:25:02.579: E/AndroidRuntime(17137):    at android.app.Fragment.instantiate(Fragment.java)
08-25 12:25:02.579: E/AndroidRuntime(17137):    at pac.gazette.MainActivity$CustomClickListener.onItemClick(MainActivity.java:56)
08-25 12:25:02.579: E/AndroidRuntime(17137):    at android.widget.AdapterView.performItemClick(AdapterView.java)
08-25 12:25:02.579: E/AndroidRuntime(17137):    at android.widget.AbsListView.performItemClick(AbsListView.java)
08-25 12:25:02.579: E/AndroidRuntime(17137):    at android.widget.ListView.performItemClick(ListView.java)
08-25 12:25:02.579: E/AndroidRuntime(17137):    at android.widget.AbsListView$PerformClick.run(AbsListView.java)
08-25 12:25:02.579: E/AndroidRuntime(17137):    at android.widget.AbsListView$1.run(AbsListView.java)
08-25 12:25:02.579: E/AndroidRuntime(17137):    at android.os.Handler.handleCallback(Handler.java)
08-25 12:25:02.579: E/AndroidRuntime(17137):    at android.os.Handler.dispatchMessage(Handler.java)
08-25 12:25:02.579: E/AndroidRuntime(17137):    at android.os.Looper.loop(Looper.java)
08-25 12:25:02.579: E/AndroidRuntime(17137):    at android.app.ActivityThread.main(ActivityThread.java)
08-25 12:25:02.579: E/AndroidRuntime(17137):    at java.lang.reflect.Method.invokeNative(Native Method)
08-25 12:25:02.579: E/AndroidRuntime(17137):    at java.lang.reflect.Method.invoke(Method.java)
08-25 12:25:02.579: E/AndroidRuntime(17137):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
08-25 12:25:02.579: E/AndroidRuntime(17137):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
08-25 12:25:02.579: E/AndroidRuntime(17137):    at dalvik.system.NativeStart.main(Native Method)
08-25 12:25:02.579: E/AndroidRuntime(17137): Caused by: java.lang.ClassNotFoundException: Didn't find class "pac.gazette.fragment_feed.xml" on path: /data/app/pac.gazette-2.apk
08-25 12:25:02.579: E/AndroidRuntime(17137):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java)
08-25 12:25:02.579: E/AndroidRuntime(17137):    at java.lang.ClassLoader.loadClass(ClassLoader.java)
08-25 12:25:02.579: E/AndroidRuntime(17137):    at java.lang.ClassLoader.loadClass(ClassLoader.java)
08-25 12:25:02.579: E/AndroidRuntime(17137):    ... 17 more

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pac.gazette"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="pac.gazette.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
1
  • Post manifest file...
    – Hariharan
    Aug 25, 2013 at 10:42

2 Answers 2

9

I would recommend that you create your own Fragment and then make your own instantiation method:

Something like that:

public class MyFragment extends Fragment {

    public MyFragment() {   
        // empty constructor  
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // here you can load whatever layout you want for your fragment
        View v = inflater.inflate(R.layout.your_fragment_layout, container, false);

        // for example set the address as a textview text, or do whatever you want with it
        TextView tv = (TextView) v.findViewById(R.id.tvAddress);
        tv.setText(getArguments().getString("address"));

        return v;
    }

    public static MyFragment newInstance(String address) {
        MyFragment myFragment = new MyFragment();

        Bundle args = new Bundle();
        args.putString("address", address);
        myFragment.setArguments(args);

        return myFragment;
    }
}

And then inside the onItemClick() method:

 // new fragment handling
  FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.content_frame, MyFragmet.newInstance(fragmentAddresses[position]);
            transaction.commit();  
6
  • Is it better to use an inflator than to instantiate the fragment? What is the difference? I also don't understand why it is necessary to bundle in a hash in these fragments. I've seen it before, but I don't understand where the call comes in for the information.
    – Colin
    Aug 25, 2013 at 12:55
  • Well, where do you define what your Fragment will look like? It has to be inflated at some point. Inflating inside onCreateView() is standard procedure. Which call for the information do you mean? As soon as the fragment is "replaced" by the FragmentMangaer, the onCreateView() gets called. Aug 25, 2013 at 12:59
  • Ah, ok. I'll try inflation/onCreateView() instead. Instead of onCreateView() I was doing Fragment.instantiate(), just because I saw one example of it that way. // About the call for information, I meant your args.putString("address", address). I understand the second address, the variable, but why the first "address", the string? The second one gets called eventually during inflation, but when does the first get called? -- is it just cosmetic? Thanks a lot, Phil.
    – Colin
    Aug 25, 2013 at 13:18
  • The Bundle takes key/value pairs. The first parameter of the putString(..., ...) method is the key, the other the value that will be stored. So the key can be anything you want, just "remember" it so that you can use it when calling getString(..., ...) to get the stored value back again with the key. I hope you understand now. It could also look like putString("iamdafknkey", address) :) Upon calling newInstance() the value gets stored in the bundle. Then later, wen onCreateView() is called, you can take out the value. Aug 25, 2013 at 13:23
  • Okay, so it's just a hash. I get that :) Thanks Phil, hopefully I'll get it running.
    – Colin
    Aug 25, 2013 at 13:25
0

I had similar problem, I fixed it with this

MyFragment.instantiate(
                    MainActivity.this, "whole.path.to.MyFragment");

@param fname The class name of the fragment to instantiate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.