gpt4 book ai didi

Android - 类不是 View android.support.v4.fragment

转载 作者:行者123 更新时间:2023-11-29 14:18:18 27 4
gpt4 key购买 nike

我必须使用支持 v4 的 fragment ,但我遇到了导致应用程序崩溃的错误。我找不到解决方案!

这是我的代码:

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

}

编辑

带有 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

带有 fragment 的 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

最佳答案

对于原生 fragment (android.app.Fragment)和支持 fragment (android.support.v4.app.Fragment),XML 布局文件中的语法是相同的.

<fragment
android:name="com.example.myfragment"
... />

但是当您在 Activity 中使用支持 fragment 时,您的 Activity 必须继承FragmentActivity(来自支持包)而不是原生Activity。另一方面,当您使用 native fragment 时,您的 Activity 可以扩展 Activity。但建议始终使用支持 fragment 而不是原生 fragment ,即使您的目标设备装有 Android 4 或更新版本。

关于Android - 类不是 View android.support.v4.fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16143135/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com