SupportFragment未在设备上显示

时间:2015-11-03 13:09:36

标签: android android-support-library

目前我正在学习android支持库,我想将SupportFragments添加到我的测试应用程序中。我实现的片段正在我的2.3.3模拟器和我的平板电脑Samsung Note 4.0.4上工作,但不在我的带有5.x的Samsung S5上。

实现的NavigationDrawer运行良好,相应的方法都被调用。当我打开抽屉时,单击第二个菜单项,调用替换片段的方法。

我希望我已经添加了所有重要的代码部分。

Logcat输出:

    11-03 14:04:26.356 4861-4861/incentivelabs.supportlibtest E/Zygote: MountEmulatedStorage()
11-03 14:04:26.356 4861-4861/incentivelabs.supportlibtest E/Zygote: v2
11-03 14:04:26.356 4861-4861/incentivelabs.supportlibtest I/SELinux: Function: selinux_compare_spd_ram, SPD-policy is existed. and_ver=SEPF_SM-G901F_5.0.2 ver=27
11-03 14:04:26.356 4861-4861/incentivelabs.supportlibtest I/SELinux: Function: selinux_compare_spd_ram , priority [1] , priority version is VE=SEPF_SM-G901F_5.0.2_0027
11-03 14:04:26.356 4861-4861/incentivelabs.supportlibtest E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
11-03 14:04:26.356 4861-4861/incentivelabs.supportlibtest I/art: Late-enabling -Xcheck:jni
11-03 14:04:26.356 4861-4861/incentivelabs.supportlibtest I/libpersona: KNOX_SDCARD checking this for 10292
11-03 14:04:26.356 4861-4861/incentivelabs.supportlibtest I/libpersona: KNOX_SDCARD not a persona
11-03 14:04:26.376 4861-4861/incentivelabs.supportlibtest D/TimaKeyStoreProvider: TimaSignature is unavailable
11-03 14:04:26.386 4861-4861/incentivelabs.supportlibtest D/ActivityThread: Added TimaKeyStore provider
11-03 14:04:26.566 4861-4878/incentivelabs.supportlibtest I/GMPM: App measurement is starting up
11-03 14:04:26.566 4861-4878/incentivelabs.supportlibtest E/GMPM: getGoogleAppId failed with status: 10
11-03 14:04:26.576 4861-4878/incentivelabs.supportlibtest E/GMPM: Uploading is not possible. App measurement disabled
11-03 14:04:26.626 4861-4861/incentivelabs.supportlibtest D/PhoneWindow: *FMB* installDecor mIsFloating : false
11-03 14:04:26.626 4861-4861/incentivelabs.supportlibtest D/PhoneWindow: *FMB* installDecor flags : -2139029248
11-03 14:04:26.656 4861-4861/incentivelabs.supportlibtest D/AbsListView: Get MotionRecognitionManager
11-03 14:04:26.686 4861-4861/incentivelabs.supportlibtest D/API: RUNNING NEW DEVICE
11-03 14:04:26.716 4861-4897/incentivelabs.supportlibtest D/OpenGLRenderer: Render dirty regions requested: true
11-03 14:04:26.726 4861-4861/incentivelabs.supportlibtest D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
11-03 14:04:26.726 4861-4861/incentivelabs.supportlibtest D/PhoneWindow: *FMB* isFloatingMenuEnabled return false
11-03 14:04:26.766 4861-4897/incentivelabs.supportlibtest I/Adreno: EGLInit: QTI Build: 02/03/15, b606245, I0829b9e471, 20150203_LA_BF_2_1_AU139
11-03 14:04:26.776 4861-4897/incentivelabs.supportlibtest I/OpenGLRenderer: Initialized EGL, version 1.4
11-03 14:04:26.786 4861-4897/incentivelabs.supportlibtest D/OpenGLRenderer: Get maximum texture size. GL_MAX_TEXTURE_SIZE is 16384
11-03 14:04:26.786 4861-4897/incentivelabs.supportlibtest D/OpenGLRenderer: Enabling debug mode 0
11-03 14:04:26.936 4861-4861/incentivelabs.supportlibtest I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@3cea95f1 time:553865839

MainActivity(包含片段):

package incentivelabs.supportlibtest;

import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;

public class MainActivity extends AppCompatActivity
        implements  BlankFragment.OnFragmentInteractionListener,
                    PlusOneFragment.OnFragmentInteractionListener,
                    NavigationView.OnNavigationItemSelectedListener
{


    Toolbar mToolbar;
    DrawerLayout mDrawer;
    NavigationView mNavViewer;

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

        mDrawer = (DrawerLayout)findViewById(R.id.drawerlayout);
        mNavViewer = (NavigationView) findViewById(R.id.navigationViewer);
        mNavViewer.setNavigationItemSelectedListener(this);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);

        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        if(actionBar != null)
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDrawer.openDrawer(GravityCompat.START);
            }
        });

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            Log.d("API", "RUNNING NEW DEVICE");
        }else{
            Log.d("API", "RUNNING OLD DEVICE");
        }

        getSupportFragmentManager().beginTransaction().add(R.id.nav_contentframe, new BlankFragment()).commit();

    }


    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        Log.d("Menu", "Button pressed");
        switch (item.getItemId()) {
            case R.id.file:
                Snackbar.make(mDrawer,"Context Menu button pressed", Snackbar.LENGTH_LONG).setAction("Action", null).show();
                return true;

            default:
                return super.onContextItemSelected(item);
        }
    }

    private void switchFragement(Fragment target){
        Log.d("Fragment", "Switching fragment");
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack so the user can navigate back
        transaction.replace(R.id.nav_contentframe, target);
        transaction.addToBackStack(null);

        // Commit the transaction
        transaction.commit();
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem menuItem) {
        menuItem.setCheckable(true);
        switch(menuItem.getItemId()){
            case R.id.navigation_item_1:
                switchFragement(new BlankFragment());
                break;

            case R.id.navigation_item_2:
                switchFragement(new PlusOneFragment());
                break;
        }
        mDrawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

布局XML文件:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawerlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity">



    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:theme="@style/AppTheme">

        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/toolbar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="false"
            android:theme="@style/AppTheme"
            android:background="@color/colorAccent"/>

    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/nav_contentframe"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize" />




    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:id="@+id/navigationViewer"
        android:divider="@color/colorAccent"
        app:headerLayout="@layout/menu_header_layout"
        app:menu="@menu/drawer_menu" />

</android.support.v4.widget.DrawerLayout>

BlankFrament(另一个看起来几乎相同)

package incentivelabs.supportlibtest;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link BlankFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link BlankFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BlankFragment extends Fragment {


    private OnFragmentInteractionListener mListener;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment BlankFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static BlankFragment newInstance(String param1, String param2) {
        BlankFragment fragment = new BlankFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    public BlankFragment() {
        // Required empty public constructor
    }

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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank, container, false);
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }

}

1 个答案:

答案 0 :(得分:0)

似乎问题是布局xml文件。 AppBarLayout占据了整个屏幕,因此片段的FrameLayout不可见(我想在屏幕底部的底部)。 我将FrameLayout放在AppBarLayout中(我不知道这是一个非常好的做法)现在一切都在我测试过的每台设备上运行良好。

<android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="fill_parent">

        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/toolbar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/darker_gray"/>

        <FrameLayout
            android:id="@+id/nav_contentframe"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="13"
            android:background="@android:color/background_light"
            />