我必须使用片段,支持v4,但是我的应用程序崩溃了。 我找不到解决方案!
这是我的代码:
activity_advert_list.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);
}
}
}
修改
Logcat with android.support.v4.app.Fragment:
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
答案 0 :(得分:13)
对于本机片段(android.app.Fragment
)和支持片段(android.support.v4.app.Fragment
),XML布局文件中的语法是相同的。
<fragment
android:name="com.example.myfragment"
... />
但是,当您在活动中使用支持片段时,您的活动必须继承FragmentActivity
(来自支持包)而不是本机Activity
。另一方面,当您使用本机片段时,您的活动可以扩展Activity
。但是,即使您的目标设备具有Android 4或更高版本,也建议始终使用支持片段而不是本机片段。
答案 1 :(得分:8)
只是以这种方式使用它的工作
<fragment
android:id="@+id/firstf"
android:name="com.example.fragments_slider.fregment_classOne"
android:layout_width="match_parent"
android:layout_height="match_parent" />
答案 2 :(得分:1)
万一有人犯了和我一样的错误。 如果您忘记添加
super.onCreate(savedInstanceState);
在FragmentActivity的onCreate方法中。你会收到错误的。