未显示Android 5.0之前版本的片段

未显示Android 5.0之前版本的片段,android,android-fragments,Android,Android Fragments,在启动时使用[推荐代码][1]启动片段时,它会在运行Android 5.0的虚拟设备上正确显示,但在运行4.4.2的手机上不会显示 我使用Android Studio中大部分自动生成的代码创建了活动 片段中的吐司在手机上被激活,所以我知道片段正在启动。它只是因为某种原因没有显示在手机上 如果您能帮助它运行,我们将不胜感激 以下是活动代码: import android.net.Uri; import android.os.Bundle; import android.support.v4.wid

在启动时使用[推荐代码][1]启动片段时,它会在运行Android 5.0的虚拟设备上正确显示,但在运行4.4.2的手机上不会显示

我使用Android Studio中大部分自动生成的代码创建了活动

片段中的吐司在手机上被激活,所以我知道片段正在启动。它只是因为某种原因没有显示在手机上

如果您能帮助它运行,我们将不胜感激

以下是活动代码:

import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.widget.Toast;



public class MainActivity extends ActionBarActivity implements NavigationDrawerCallbacks, UserOverviewFragment.OnFragmentInteractionListener {

    private static final String PREF_FIRST_RUN = "false";

    private Toolbar mToolbar;
    private NavigationDrawerFragment mNavigationDrawerFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        // TODO: get this working.
        // Check that the activity is using the layout version with
        // the fragment_container FrameLayout
        if (findViewById(R.id.container) != null) {

            // However, if we're being restored from a previous state,
            // then we don't need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState != null) {
                return;
            }

            // Create a new Fragment to be placed in the activity layout
            UserOverviewFragment firstFragment = new UserOverviewFragment();



            // In case this activity was started with special instructions from an
            // Intent, pass the Intent's extras to the fragment as arguments
//            firstFragment.setArguments(getIntent().getExtras());

            // Add the fragment to the 'fragment_container' FrameLayout
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, firstFragment).commit();
        }


        // TODO: Do something the first time the app is started.


        mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
        mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO: invalidate menu as soon as the drawer slides a certain amount
        if(mNavigationDrawerFragment.isDrawerOpen()) {
            invalidateOptionsMenu();
        }else
        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);

    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        Toast.makeText(this, "Menu item selected -> " + position, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onBackPressed() {
        if (mNavigationDrawerFragment.isDrawerOpen())
            mNavigationDrawerFragment.closeDrawer();
        else
            super.onBackPressed();
    }




    public void changeMainFragment(){

    }

    public boolean checkForFirstRun(){
        String firstRun = mNavigationDrawerFragment.readSharedSetting(getBaseContext(), PREF_FIRST_RUN, "true");
        if(firstRun == "true"){
            mNavigationDrawerFragment.saveSharedSetting(getBaseContext(), PREF_FIRST_RUN, "false");
            return true;
        }
        else return false;
    }

    public void onFirstRun(){
        // TODO: Stuff to do just once, the first time the app is run. Possibly not necessary.


        // TODO: Delete this next line after testing.
        Toast.makeText(this, "First Time!!", Toast.LENGTH_LONG).show();
    }

    private void determineStartingFragment() {

    }


    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}
活动布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/toolbar_actionbar"
        layout="@layout/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar_actionbar">

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:clickable="true"
            android:layout_height="match_parent"/>

        <!-- android:layout_marginTop="?android:attr/actionBarSize"-->
        <fragment
            android:id="@+id/fragment_drawer"
            android:name="com.king_kingdom.mathiness.NavigationDrawerFragment"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:layout="@layout/fragment_navigation_drawer"
            tools:layout="@layout/fragment_main" />
    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>

感谢@papium上传build.gradle。现在,我建议更改该文件: 来自:编译'com.android.support:appcompat-v7:21.0.3'

编译com.android.support:appcompat-v7:*18.0.0**


但是,这会影响API 21和未来的Android版本。另外,也许你可以看到NavigationDrawer,但看不到ActionBar。

最后,这是一个简单的样式问题。在我的样式代码中,我意外地将背景和文本颜色都指定为白色。当然,这意味着你看不到它


我不太清楚为什么它会出现在模拟器中,除了API v21中可能有一个默认样式绕过了我的app.theme。

我从未使用过NavigationDrawer。然而,我知道支持Android 5(API 21)和API 21之前版本的ActionBarActivity是很棘手的。因此,您需要查看build.gradle文件并检查编译依赖项和SDK版本。因此,只需上传该文件进行分析。添加了gradle构建文件。谢谢你的关心。
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;
import android.widget.Toast;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link UserOverviewFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link UserOverviewFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class UserOverviewFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    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 user_overview.
     */
    // TODO: Rename and change types and number of parameters
    public static UserOverviewFragment newInstance(String param1, String param2) {
        UserOverviewFragment fragment = new UserOverviewFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        Toast.makeText(getActivity().getBaseContext(),"This is the User Overview Fragment", Toast.LENGTH_LONG).show();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_user_overview, 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);
    }

}
<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:orientation="vertical"
    tools:context="com.king_kingdom.mathiness.UserOverviewFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="User Overview"
        android:textSize="@dimen/abc_text_size_large_material"
        android:longClickable="false"
        android:layout_marginTop="109dp" />

</LinearLayout>
apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.king_kingdom.toolbar_test"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
}