Android - class is not a view android.support.v4.f

2019-08-12 03:32发布

I have to use fragment, with v4 support, but I've got an error crashing the App. I can't find the solution!

This is my code :

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

}

EDIT

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 with fragment

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

3条回答
三岁会撩人
2楼-- · 2019-08-12 03:52

just in case someone make the same mistake as i do. If you forget to add

super.onCreate(savedInstanceState);

in FragmentActivity's onCreate method. you'll receive the mistake.

查看更多
Ridiculous、
3楼-- · 2019-08-12 03:58

For both native fragments (android.app.Fragment) and support fragments (android.support.v4.app.Fragment), syntax in XML layout file is identical.

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

But when you use support fragments in an activity, your activity must inherits FragmentActivity(from support package) not native Activity. On the other hand, when you're using native fragments, your activity could extends Activity. But it's recommended that always use support fragments instead of native fragments even if your target device has Android 4 or newer.

查看更多
我只想做你的唯一
4楼-- · 2019-08-12 04:00

just use this way its work

<fragment
    android:id="@+id/firstf"
    android:name="com.example.fragments_slider.fregment_classOne"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
查看更多
登录 后发表回答