简体   繁体   English

片段活动不膨胀

[英]Fragment activity not inflating

For my master detail flow app I'm trying to inflate my Continents list view on my tablet but it won't do so and I end up with these errors in my log cat. 对于我的主要详细信息流应用程序,我试图在平板电脑上添加我的“大陆列表”视图,但这样做没有成功,最终我在日志猫中遇到了这些错误。 Does anyone know what I'm doing wrong? 有人知道我在做什么错吗?

Continents List xml 大陆列表xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp" android:baselineAligned="false"
    android:divider="?android:attr/dividerHorizontal" android:orientation="horizontal"
    android:showDividers="middle" tools:context=".ItemListActivity">

    <fragment android:id="@+id/continents_list" android:name="com.apptacularapps.md.ContinentsListFragment"
        android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"
        tools:layout="@android:layout/list_content" />

    <FrameLayout android:id="@+id/item_detail_container" android:layout_width="0dp"
        android:layout_height="match_parent" android:layout_weight="3" />

</LinearLayout>

ContinentsListActivity.java ContinentsListActivity.java

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;

public class ContinentsListActivity extends ActionBarActivity
        implements ContinentsListFragment.Callbacks {

    private boolean mTwoPane;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_continents_list);

        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.continents_list_contaniner, new ContinentsListFragment());
        ft.commit();

        if (findViewById(R.id.item_detail_container) != null) {
            mTwoPane = true;

            ((ContinentsListFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.continents_list))
                    .setActivateOnItemClick(true);
        }

    }

    @Override
    public void onItemSelected(String id) {
        if("1".equals(id)){
            if (mTwoPane) {

                Bundle arguments = new Bundle();
                arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id);
                ItemDetailFragment fragment = new ItemDetailFragment();
                fragment.setArguments(arguments);
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.continents_list, fragment)
                        .commit();

            } else {
                Intent detailIntent = new Intent(this, ContinentsListActivity.class);
                startActivity(detailIntent);
            }
        }
    }
}

ContinentsListFragment.java ContinentsListFragment.java

    package com.apptacularapps.md;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ContinentsListFragment extends ListFragment {

    private static final String STATE_ACTIVATED_POSITION = "activated_position";

    private Callbacks mCallbacks = sContinentsCallbacks;

    private int mActivatedPosition = ListView.INVALID_POSITION;

    public interface Callbacks {
        public void onItemSelected(String id);
    }

    private static Callbacks sContinentsCallbacks = new Callbacks() {
        @Override
        public void onItemSelected(String id) {
        }
    };

    public ContinentsListFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // TODO: replace with a real list adapter.
        setListAdapter(new ArrayAdapter<ContinentsContent.ContinentsItem>(
                getActivity(),
                android.R.layout.simple_list_item_activated_1,
                android.R.id.text1,
                ContinentsContent.ITEMS));
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        if (savedInstanceState != null
                && savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
            setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION));
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // Activities containing this fragment must implement its callbacks.
        if (!(activity instanceof Callbacks)) {
            throw new IllegalStateException("Activity must implement fragment's callbacks.");
        }

        mCallbacks = (Callbacks) activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();

        // Reset the active callbacks interface to the Continents implementation.
        mCallbacks = sContinentsCallbacks;
    }

    @Override
    public void onListItemClick(ListView listView, View view, int position, long id) {
        super.onListItemClick(listView, view, position, id);

        mCallbacks.onItemSelected(ContinentsContent.ITEMS.get(position).id);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mActivatedPosition != ListView.INVALID_POSITION) {
            outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
        }
    }

    public void setActivateOnItemClick(boolean activateOnItemClick) {
        getListView().setChoiceMode(activateOnItemClick
                ? ListView.CHOICE_MODE_SINGLE
                : ListView.CHOICE_MODE_NONE);
    }

    private void setActivatedPosition(int position) {
        if (position == ListView.INVALID_POSITION) {
            getListView().setItemChecked(mActivatedPosition, false);
        } else {
            getListView().setItemChecked(position, true);
        }

        mActivatedPosition = position;
    }
}

Logcat Logcat

    05-20 20:29:42.523  32338-32338/com.apptacularapps.md E/libprocessgroup﹕ failed to make and chown /acct/uid_10056: Read-only file system
05-20 20:29:42.531  32338-32338/com.apptacularapps.md W/Zygote﹕ createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
05-20 20:29:42.532  32338-32338/com.apptacularapps.md I/art﹕ Not late-enabling -Xcheck:jni (already on)
05-20 20:29:43.002  32338-32352/com.apptacularapps.md I/art﹕ Background sticky concurrent mark sweep GC freed 3104(261KB) AllocSpace objects, 0(0B) LOS objects, 26% free, 829KB/1135KB, paused 11.699ms total 50.909ms
05-20 20:29:43.006  32338-32359/com.apptacularapps.md D/OpenGLRenderer﹕ Render dirty regions requested: true
05-20 20:29:43.034  32338-32338/com.apptacularapps.md D/﹕ HostConnection::get() New Host Connection established 0xa693a490, tid 32338
05-20 20:29:43.070  32338-32338/com.apptacularapps.md D/Atlas﹕ Validating map...
05-20 20:29:43.227  32338-32359/com.apptacularapps.md D/﹕ HostConnection::get() New Host Connection established 0xa693a8a0, tid 32359
05-20 20:29:43.246  32338-32359/com.apptacularapps.md I/OpenGLRenderer﹕ Initialized EGL, version 1.4
05-20 20:29:43.376  32338-32359/com.apptacularapps.md D/OpenGLRenderer﹕ Enabling debug mode 0
05-20 20:29:43.397  32338-32359/com.apptacularapps.md W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-20 20:29:43.397  32338-32359/com.apptacularapps.md W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa696eba0, error=EGL_SUCCESS
05-20 20:29:46.874  32338-32338/com.apptacularapps.md D/AndroidRuntime﹕ Shutting down VM
05-20 20:29:46.875  32338-32338/com.apptacularapps.md E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.apptacularapps.md, PID: 32338
    java.lang.IllegalStateException: Activity must implement fragment's callbacks.
            at com.apptacularapps.md.ContinentsListFragment.onAttach(ContinentsListFragment.java:89)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:907)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

在此处输入图片说明

your implementation of onAttach , says 您对onAttach的实现,说

if (!(activity instanceof Callbacks)) {
     throw new IllegalStateException("Activity must implement fragment's callbacks.");
}

To fix you have to add implements Callbacks to the Activity hosting the Fragment. 要解决此问题,您必须向托管Fragment的Activity添加implements Callbacks

Edit: 编辑:

in the xml of your activity, replace 在您活动的xml中,替换

  <fragment android:id="@+id/continents_list" android:name="com.apptacularapps.md.ContinentsListFragment"
        android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"
        tools:layout="@android:layout/list_content" />

with

 <FrameLayout android:id="@+id/continents_list_contaniner" 
        android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/>

and onCreate() of the Activity, after setContentView do: setContentView之后执行Activity的onCreate():

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.continents_list_contaniner, new ContinentsListFragment());
ft.commit();

Edit2 : 编辑2

in your Activity replace 在您的活动中替换

import android.app.FragmentManager;
import android.app.FragmentTransaction;

with

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 片段中的错误夸大活动:InflateException - Error Inflating Activity from Fragment: InflateException 使用导航活动在 Recyclerview 中膨胀 Class 片段时出错 - Error inflating Class Fragment in Recyclerview with Navigation Activity 错误在活动映射xml中膨胀类片段 - Error Inflating class fragment in activity maps xml 在片段中膨胀但在活动中工作时出错 - Error while inflating in fragment but works in an Activity 在主要活动中膨胀片段会带来错误 - Inflating a fragment in main activity brings an error 测试片段活动时膨胀类片段时出错 - Error inflating class fragment While testing out fragment Activity 从 android.app.Activity 膨胀 androidx 片段时出现 Fragment$InstantiationException - Fragment$InstantiationException on inflating androidx fragment from android.app.Activity PreferenceFragmentCompat Fragment 在活动 OnCreate 期间有 mPreferenceManager null 充气后 - PreferenceFragmentCompat Fragment has mPreferenceManager null during activity OnCreate after inflating 无法恢复活动NULL POINTER EXCEPTION&错误夸大类片段? - unable to resume activity NULL POINTER EXCEPTION & Error inflating class fragment? 膨胀 Fragment 时出现 StackoverflowError - StackoverflowError while inflating Fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM