Domanda

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>
È stato utile?

Soluzione

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();  

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top