这是我的第一篇文章,所以我提前道歉......
我正在尝试编译一个简单的抽屉示例/实践应用程序。但是,当我单击列表视图中的项目时,它会崩溃。Logcat 说它无法从 android.app.Fragment 实例化。我也尝试过使用 android.support.v4 库——假设我正确地实现了它,这是可疑的——结果相同。坦率地说,我不确定有什么区别。我完全被难住了。请帮忙!
主要活动:
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>
日志猫:
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>