سؤال

I'm trying to implement a Navigation Drawer using FragmentActivity.
And I need to have the application in FullScreen mode.

The application throws a RuntimeException whenever I add this line of code in manifest

android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"

If I remove this line, then it works well !

Here's my code:

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;
    }

}

And Here's the error log

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

Why I cannot have full screen for this fragment activity which contains navigation drawer ?


BTW, i tried to add

android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"

to the activity itself not the application, but it throws the same error.


And I tried to add these lines on code

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);

to MainActivity.oncreate before setContentView(R.layout.activity_main) but it throws the same error

هل كانت مفيدة؟

المحلول

The solution for those who are interested is to change

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActionBar().getThemedContext(), R.layout.drawer_list_item, data);

so that it will be

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.drawer_list_item, data);

Because ActionBar is no longer available in full screen mode

نصائح أخرى

getActionBar() will return null. You have

android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"

public ActionBar getActionBar ()

Added in API level 11 Retrieve a reference to this activity's ActionBar.

Returns The Activity's ActionBar, or null if it does not have one.

Try to use

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

to make your activity full screened And than use theme, that provide actionBar, for example : @android:style/Theme.Holo.Light.DarkActionBar

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top