我試圖使用FragmentActivity
實現Navigation Drawer
。
而我需要在FullScreen
模式的應用程序。FragmentActivity和FullScreen中的NavigationDrawer引發RuntimeException
應用程序將引發RuntimeException
每當我在清單
機器人添加這行代碼:主題= 「@安卓風格/ Theme.Black.NoTitleBar.Fullscreen」
如果我刪除這一行,然後它運作良好!
這裏是我的代碼:
的manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
<activity
android:name="com.sample.app.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml中
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<FrameLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:id="@+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#FFFFFF"
android:choiceMode="singleChoice"/>
</android.support.v4.widget.DrawerLayout>
MainActivity.java
package com.sample.app;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends FragmentActivity {
final String[] data ={"one","two","three"};
final String[] fragments ={
"com.sample.app.FragmentOne"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActionBar().getThemedContext(), R.layout.drawer_list_item, data);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView navList = (ListView) findViewById(R.id.drawer);
navList.setAdapter(adapter);
navList.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
drawer.setDrawerListener(new DrawerLayout.SimpleDrawerListener(){
@Override
public void onDrawerClosed(View drawerView){
super.onDrawerClosed(drawerView);
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.main, Fragment.instantiate(MainActivity.this, fragments[pos]));
tx.commit();
}
});
drawer.closeDrawer(navList);
}
});
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.main,Fragment.instantiate(MainActivity.this, fragments[0]));
tx.commit();
}
}
fragment_one.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="one" />
</RelativeLayout>
FragmentOne.java
package com.sample.app;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentOne extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_one, null);
return root;
}
}
和這裏的錯誤日誌
03-27 10:09:47.384: E/AndroidRuntime(993): FATAL EXCEPTION: main
03-27 10:09:47.384: E/AndroidRuntime(993): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample.app/com.sample.app.MainActivity}: java.lang.NullPointerException
03-27 10:09:47.384: E/AndroidRuntime(993): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-27 10:09:47.384: E/AndroidRuntime(993): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-27 10:09:47.384: E/AndroidRuntime(993): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-27 10:09:47.384: E/AndroidRuntime(993): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-27 10:09:47.384: E/AndroidRuntime(993): at android.os.Handler.dispatchMessage(Handler.java:99)
03-27 10:09:47.384: E/AndroidRuntime(993): at android.os.Looper.loop(Looper.java:137)
03-27 10:09:47.384: E/AndroidRuntime(993): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-27 10:09:47.384: E/AndroidRuntime(993): at java.lang.reflect.Method.invokeNative(Native Method)
03-27 10:09:47.384: E/AndroidRuntime(993): at java.lang.reflect.Method.invoke(Method.java:511)
03-27 10:09:47.384: E/AndroidRuntime(993): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-27 10:09:47.384: E/AndroidRuntime(993): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-27 10:09:47.384: E/AndroidRuntime(993): at dalvik.system.NativeStart.main(Native Method)
03-27 10:09:47.384: E/AndroidRuntime(993): Caused by: java.lang.NullPointerException
03-27 10:09:47.384: E/AndroidRuntime(993): at com.sample.app.MainActivity.onCreate(MainActivity.java:32)
03-27 10:09:47.384: E/AndroidRuntime(993): at android.app.Activity.performCreate(Activity.java:5104)
03-27 10:09:47.384: E/AndroidRuntime(993): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-27 10:09:47.384: E/AndroidRuntime(993): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-27 10:09:47.384: E/AndroidRuntime(993): ... 11 more
爲什麼我無法全屏顯示包含導航抽屜的片段活動?
BTW,我試圖
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
添加到活動本身而不是應用程序,但它拋出了同樣的錯誤。
我試着setContentView(R.layout.activity_main)
之前代碼
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setDisplayShowHomeEnabled(false);
((FragmentBreadCrumbs)(findViewById(android.R.id.title))).setVisibility(View.GONE);
添加這些行MainActivity.oncreate
但它拋出了同樣的錯誤
但是導航抽屜根本沒有顯示。我認爲這是一個不好的選擇。請檢查設計指南https://developer.android.com/design/patterns/navigation-drawer.html – Raghunandan