我必须使用带有 v4 支持的片段,但我在应用程序崩溃时出错。我找不到解决方案!
这是我的代码:
活动广告列表.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<android.support.v4.app.Fragment
android:id="@+id/advertListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.myapp.fragment.AdvertListFragment"/>
AdvertListActivity.java
package com.myapp.app;
import java.util.ArrayList;
import com.myapp.R;
import com.myapp.classmodel.Advert;
import com.myapp.fragment.AdvertDetailFragment;
import com.myapp.interf.OnAdvertSelectedListener;
import com.google.analytics.tracking.android.EasyTracker;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class AdvertListActivity extends FragmentActivity implements OnAdvertSelectedListener {
private AdvertListActivity activity = this;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_advert_list);
}
@Override
public void onStart() {
super.onStart();
// Google Analytics
EasyTracker.getInstance().activityStart(this);
}
@Override
public void onStop() {
super.onStop();
// Google Analytics
EasyTracker.getInstance().activityStop(this);
}
/**
* Called by fragment
* Used to pass advertList & current position to detail fragment
*/
@Override
public void onAdvertItemSelected(ArrayList<Advert> advertList, int currentPosition) {
AdvertDetailFragment fragment = (AdvertDetailFragment) activity.getSupportFragmentManager().findFragmentById(R.id.advertDetailFragment);
if (fragment != null && fragment.isInLayout()) {
fragment.setData(advertList, currentPosition);
}
}
}
编辑
带有 android.support.v4.app.Fragment 的 Logcat:
04-22 11:09:48.223: E/AndroidRuntime(30349): FATAL EXCEPTION: main
04-22 11:09:48.223: E/AndroidRuntime(30349): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.app.AdvertListActivity}: android.view.InflateException: Binary XML file line #7: Class is not a View android.support.v4.app.Fragment
04-22 11:09:48.223: E/AndroidRuntime(30349): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-22 11:09:48.223: E/AndroidRuntime(30349): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-22 11:09:48.223: E/AndroidRuntime(30349): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-22 11:09:48.223: E/AndroidRuntime(30349): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-22 11:09:48.223: E/AndroidRuntime(30349): at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 11:09:48.223: E/AndroidRuntime(30349): at android.os.Looper.loop(Looper.java:137)
04-22 11:09:48.223: E/AndroidRuntime(30349): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-22 11:09:48.223: E/AndroidRuntime(30349): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 11:09:48.223: E/AndroidRuntime(30349): at java.lang.reflect.Method.invoke(Method.java:511)
04-22 11:09:48.223: E/AndroidRuntime(30349): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-22 11:09:48.223: E/AndroidRuntime(30349): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-22 11:09:48.223: E/AndroidRuntime(30349): at dalvik.system.NativeStart.main(Native Method)
04-22 11:09:48.223: E/AndroidRuntime(30349): Caused by: android.view.InflateException: Binary XML file line #7: Class is not a View android.support.v4.app.Fragment
04-22 11:09:48.223: E/AndroidRuntime(30349): at android.view.LayoutInflater.createView(LayoutInflater.java:604)
04-22 11:09:48.223: E/AndroidRuntime(30349): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
04-22 11:09:48.223: E/AndroidRuntime(30349): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
04-22 11:09:48.223: E/AndroidRuntime(30349): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-22 11:09:48.223: E/AndroidRuntime(30349): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-22 11:09:48.223: E/AndroidRuntime(30349): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-22 11:09:48.223: E/AndroidRuntime(30349): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
04-22 11:09:48.223: E/AndroidRuntime(30349): at android.app.Activity.setContentView(Activity.java:1881)
04-22 11:09:48.223: E/AndroidRuntime(30349): at com.myapp.app.AdvertListActivity.onCreate(AdvertListActivity.java:19)
04-22 11:09:48.223: E/AndroidRuntime(30349): at android.app.Activity.performCreate(Activity.java:5104)
04-22 11:09:48.223: E/AndroidRuntime(30349): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-22 11:09:48.223: E/AndroidRuntime(30349): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-22 11:09:48.223: E/AndroidRuntime(30349): ... 11 more
04-22 11:09:48.223: E/AndroidRuntime(30349): Caused by: java.lang.ClassCastException: android.support.v4.app.Fragment cannot be cast to android.view.View
04-22 11:09:48.223: E/AndroidRuntime(30349): at java.lang.Class.asSubclass(Class.java:1380)
04-22 11:09:48.223: E/AndroidRuntime(30349): at android.view.LayoutInflater.createView(LayoutInflater.java:552)
04-22 11:09:48.223: E/AndroidRuntime(30349): ... 22 more
带片段的 Logcat
04-22 11:07:39.950: E/AndroidRuntime(30107): FATAL EXCEPTION: main
04-22 11:07:39.950: E/AndroidRuntime(30107): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.app.AdvertListActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
04-22 11:07:39.950: E/AndroidRuntime(30107): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-22 11:07:39.950: E/AndroidRuntime(30107): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-22 11:07:39.950: E/AndroidRuntime(30107): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-22 11:07:39.950: E/AndroidRuntime(30107): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-22 11:07:39.950: E/AndroidRuntime(30107): at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 11:07:39.950: E/AndroidRuntime(30107): at android.os.Looper.loop(Looper.java:137)
04-22 11:07:39.950: E/AndroidRuntime(30107): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-22 11:07:39.950: E/AndroidRuntime(30107): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 11:07:39.950: E/AndroidRuntime(30107): at java.lang.reflect.Method.invoke(Method.java:511)
04-22 11:07:39.950: E/AndroidRuntime(30107): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-22 11:07:39.950: E/AndroidRuntime(30107): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-22 11:07:39.950: E/AndroidRuntime(30107): at dalvik.system.NativeStart.main(Native Method)
04-22 11:07:39.950: E/AndroidRuntime(30107): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
04-22 11:07:39.950: E/AndroidRuntime(30107): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
04-22 11:07:39.950: E/AndroidRuntime(30107): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
04-22 11:07:39.950: E/AndroidRuntime(30107): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-22 11:07:39.950: E/AndroidRuntime(30107): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-22 11:07:39.950: E/AndroidRuntime(30107): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-22 11:07:39.950: E/AndroidRuntime(30107): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
04-22 11:07:39.950: E/AndroidRuntime(30107): at android.app.Activity.setContentView(Activity.java:1881)
04-22 11:07:39.950: E/AndroidRuntime(30107): at com.myapp.app.AdvertListActivity.onCreate(AdvertListActivity.java:19)
04-22 11:07:39.950: E/AndroidRuntime(30107): at android.app.Activity.performCreate(Activity.java:5104)
04-22 11:07:39.950: E/AndroidRuntime(30107): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-22 11:07:39.950: E/AndroidRuntime(30107): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-22 11:07:39.950: E/AndroidRuntime(30107): ... 11 more
04-22 11:07:39.950: E/AndroidRuntime(30107): Caused by: java.lang.NullPointerException
04-22 11:07:39.950: E/AndroidRuntime(30107): at com.myapp.fragment.AdvertListFragment.onCreateView(AdvertListFragment.java:63)
04-22 11:07:39.950: E/AndroidRuntime(30107): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:846)
04-22 11:07:39.950: E/AndroidRuntime(30107): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1061)
04-22 11:07:39.950: E/AndroidRuntime(30107): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1160)
04-22 11:07:39.950: E/AndroidRuntime(30107): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
04-22 11:07:39.950: E/AndroidRuntime(30107): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
04-22 11:07:39.950: E/AndroidRuntime(30107): ... 21 more