如何修复InstantiationException,无法实例化Fragment?

时间:2013-08-25 10:39:15

标签: android android-fragments navigation-drawer fragmenttransaction instantiationexception

这是我的第一篇文章,所以我提前道歉......

我正在尝试编译一个简单的Drawer示例/练习应用程序。但是,当我点击列表视图中的项目时,它会崩溃。 Logcat说它无法从android.app.Fragment实例化。我也试过使用android.support.v4库 - 假设我正确实现了它,这是可疑的 - 具有相同的结果。坦率地说,我不确定区别是什么。我完全难过了。请帮忙!

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

清单:

<?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>

2 个答案:

答案 0 :(得分:5)

我建议您创建自己的片段,然后制作自己的实例化方法:

类似的东西:

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;
    }
}

然后在onItemClick()方法中:

 // new fragment handling
  FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.content_frame, MyFragmet.newInstance(fragmentAddresses[position]);
            transaction.commit();  

答案 1 :(得分:0)

我有类似的问题,我用这个修复了

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

@param fname要实例化的片段的类名。