使用[推荐代码] [1]在启动时启动片段时,它会在运行Android 5.0的虚拟设备上正常显示,但不会在运行4.4.2的手机上显示。
我使用Android Studio中自动生成的代码创建了活动。
片段中的 Toast 会在手机上激活,因此我知道片段正在启动。由于某种原因,它只是没有在手机上显示。
任何帮助它运行的人都将不胜感激。
这是活动代码:
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>
片段代码:
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>
更新:Gradle文件
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'
}
答案 0 :(得分:0)
感谢@papium上传 build.gradle 。现在,我建议更改该文件: 发件人:编译&#39; com.android.support:appcompat-v7:21.0.3&#39;
要:compile ‘com.android.support:appcompat-v7:**18.0.0**
然而,这会影响API 21和未来的Android版本。另外,也许你可以看到NavigationDrawer而不是你的ActionBar。
答案 1 :(得分:0)
最后,这是一个简单的样式问题。在我的样式代码中,我不小心将背景和文本颜色分配为白色。当然,这意味着你无法看到它。
我不太清楚为什么它会在模拟器中显示,除了API v21中可能有一个默认样式绕过我的app.theme。