我是Android新手并尝试制作简单的App。我试图在Fragments下显示数组列表。此列表会随着网站的XML数据驱动而不断更新。 SInce数组列表是动态我使用onActivityCreated而不是onCreateView。 以下是我的代码
package com.example.myearthquake;
public class EarthquakeListFragment extends ListFragment {
ArrayAdapter<Quake> aa;
ArrayList<Quake> earthquakes = new ArrayList<Quake>();
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
int layoutID = android.R.layout.simple_list_item_1;
aa = new ArrayAdapter<Quake>(getActivity(), layoutID , earthquakes);
setListAdapter(aa);
Log.d("EarthQuake","onActivityCreated");
}
MainActivity Class
public class Earthquake extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("EarthQuake","Activity");
}
布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.paad.myearthquake.EarthquakeListFragment"
android:id="@+id/EarthquakeListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
错误
01-26 16:12:12.865: D/AndroidRuntime(1480): Shutting down VM
01-26 16:12:12.865: W/dalvikvm(1480): threadid=1: thread exiting with uncaught exception (group=0x41465700)
01-26 16:12:12.906: E/AndroidRuntime(1480): FATAL EXCEPTION: main
01-26 16:12:12.906: E/AndroidRuntime(1480): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myearthquake/com.example.myearthquake.Earthquake}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
01-26 16:12:12.906: E/AndroidRuntime(1480): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
01-26 16:12:12.906: E/AndroidRuntime(1480): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-26 16:12:12.906: E/AndroidRuntime(1480): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-26 16:12:12.906: E/AndroidRuntime(1480): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-26 16:12:12.906: E/AndroidRuntime(1480): at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 16:12:12.906: E/AndroidRuntime(1480): at android.os.Looper.loop(Looper.java:137)
01-26 16:12:12.906: E/AndroidRuntime(1480): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-26 16:12:12.906: E/AndroidRuntime(1480): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 16:12:12.906: E/AndroidRuntime(1480): at java.lang.reflect.Method.invoke(Method.java:525)
01-26 16:12:12.906: E/AndroidRuntime(1480): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-26 16:12:12.906: E/AndroidRuntime(1480): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-26 16:12:12.906: E/AndroidRuntime(1480): at dalvik.system.NativeStart.main(Native Method)
01-26 16:12:12.906: E/AndroidRuntime(1480): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
01-26 16:12:12.906: E/AndroidRuntime(1480): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
01-26 16:12:12.906: E/AndroidRuntime(1480): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
01-26 16:12:12.906: E/AndroidRuntime(1480): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
01-26 16:12:12.906: E/AndroidRuntime(1480): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-26 16:12:12.906: E/AndroidRuntime(1480): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
01-26 16:12:12.906: E/AndroidRuntime(1480): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
01-26 16:12:12.906: E/AndroidRuntime(1480): at android.app.Activity.setContentView(Activity.java:1895)
01-26 16:12:12.906: E/AndroidRuntime(1480): at com.example.myearthquake.Earthquake.onCreate(Earthquake.java:13)
01-26 16:12:12.906: E/AndroidRuntime(1480): at android.app.Activity.performCreate(Activity.java:5133)
01-26 16:12:12.906: E/AndroidRuntime(1480): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-26 16:12:12.906: E/AndroidRuntime(1480): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
01-26 16:12:12.906: E/AndroidRuntime(1480): ... 11 more
01-26 16:12:12.906: E/AndroidRuntime(1480): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.paad.myearthquake.EarthquakeListFragment: make sure class name exists, is public, and has an empty constructor that is public
01-26 16:12:12.906: E/AndroidRuntime(1480): at android.app.Fragment.instantiate(Fragment.java:592)
01-26 16:12:12.906: E/AndroidRuntime(1480): at android.app.Fragment.instantiate(Fragment.java:560)
01-26 16:12:12.906: E/AndroidRuntime(1480): at android.app.Activity.onCreateView(Activity.java:4738)
01-26 16:12:12.906: E/AndroidRuntime(1480): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
01-26 16:12:12.906: E/AndroidRuntime(1480): ... 21 more
01-26 16:12:12.906: E/AndroidRuntime(1480): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.paad.myearthquake.EarthquakeListFragment" on path: DexPathList[[zip file "/data/app/com.example.myearthquake-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.myearthquake-2, /system/lib]]
01-26 16:12:12.906: E/AndroidRuntime(1480): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
01-26 16:12:12.906: E/AndroidRuntime(1480): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
01-26 16:12:12.906: E/AndroidRuntime(1480): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
01-26 16:12:12.906: E/AndroidRuntime(1480): at android.app.Fragment.instantiate(Fragment.java:582)
01-26 16:12:12.906: E/AndroidRuntime(1480): ... 24 more
01-26 16:17:13.400: I/Process(1480): Sending signal. PID: 1480 SIG: 9
答案 0 :(得分:0)
Caused by: java.lang.ClassNotFoundException:
Didn't find class "com.paad.myearthquake.EarthquakeListFragment" ...
代码中的软件包名称为com.example.myearthquake
,同时尝试加载com.paad.myearthquake
中的类。
确保XML和代码在软件包名称上达成一致。在这里:
<fragment android:name="com.paad.myearthquake.EarthquakeListFragment"
在这里:
package com.example.myearthquake;