0

Please see this code. According to me , everything is fine. I am getting a NullPointerException. This is what logcat is showing:-

07-09 17:10:37.463: E/AndroidRuntime(16400): FATAL EXCEPTION: main
07-09 17:10:37.463: E/AndroidRuntime(16400): Process: com.qanda, PID: 16400
07-09 17:10:37.463: E/AndroidRuntime(16400): Theme: themes:{default=overlay:system, iconPack:system, fontPkg:system, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system}
07-09 17:10:37.463: E/AndroidRuntime(16400): java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window android.app.Activity.getWindow()' on a null object reference
07-09 17:10:37.463: E/AndroidRuntime(16400):    at android.support.v7.app.ActionBarDrawerToggle$DrawerArrowDrawableToggle.isLayoutRtl(ActionBarDrawerToggle.java:489)
07-09 17:10:37.463: E/AndroidRuntime(16400):    at android.support.v7.app.DrawerArrowDrawable.draw(DrawerArrowDrawable.java:113)
07-09 17:10:37.463: E/AndroidRuntime(16400):    at android.widget.ImageView.onDraw(ImageView.java:1246)
07-09 17:10:37.463: E/AndroidRuntime(16400):    at android.view.View.draw(View.java:16187)
07-09 17:10:37.463: E/AndroidRuntime(16400):    at android.view.View.updateDisplayListIfDirty(View.java:15184)

Here is my activity java code:-

package com.qanda;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

@SuppressWarnings("deprecation")
public class HomeActivity extends ActionBarActivity {
    FragmentManager fragManager;
    DrawerLayout dl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        dl = (DrawerLayout) findViewById(R.id.dlhome);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarhome);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Home");
        Fragment_Navigation nav = new Fragment_Navigation();
        nav.setUp(dl, toolbar);
        String [] item1 = {"Home" , "Paytm Wallet"  , "Categories","Ask a Question" , "Be an Expert"};
        ListView lv = (ListView) findViewById(R.id.drawerlist_1);
        ListView_Adapter adapter1 = new ListView_Adapter(this, item1);
        lv.setAdapter(adapter1);
        Fragment_Home frag = new Fragment_Home();
        fragManager = getSupportFragmentManager();
        fragManager.beginTransaction().replace(R.id.frameLayout, frag).commit();
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Fragment frag;
                if(position==0){
                    //getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.string.color_list_2)));
                    getSupportActionBar().setTitle("Home");
                    frag = new Fragment_Home();
                    fragManager.beginTransaction().replace(R.id.frameLayout, frag).commit();

                }
                else if(position==1){
                    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(getResources().getString(R.string.color_list_2))));
                    frag = new Fragment_PaytmWallet();
                    fragManager.beginTransaction().replace(R.id.frameLayout, frag).commit();
                    getSupportActionBar().setTitle("PayTM Wallet");
                    if(Build.VERSION.SDK_INT>=21){
                        Window window = getWindow();
                        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                        window.setStatusBarColor(Color.parseColor(getResources().getString(R.string.color_list_primary_2)));
                    }
                }else if(position==2){
                    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(getResources().getString(R.string.color_list_3))));
                    frag = new Fragment_Categories();
                    fragManager.beginTransaction().replace(R.id.frameLayout, frag).commit();
                    getSupportActionBar().setTitle("Categories");
                    if(Build.VERSION.SDK_INT>=21){
                        Window window = getWindow();
                        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        window.setStatusBarColor(Color.parseColor(getResources().getString(R.string.color_list_primary_3)));
                    }
                }else if(position==3){
                    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(getResources().getString(R.string.color_list_4))));
                    frag = new Fragment_AskAQues();
                    fragManager.beginTransaction().replace(R.id.frameLayout, frag).commit();
                    getSupportActionBar().setTitle("Ask a Question");
                    if(Build.VERSION.SDK_INT>=21){
                        Window window = getWindow();
                        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        window.setStatusBarColor(Color.parseColor(getResources().getString(R.string.color_list_primary_4)));
                    }
                    }else if(position==4){
                    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(getResources().getString(R.string.color_list_5))));
                    frag = new Fragment_BeAnExpert();
                    fragManager.beginTransaction().replace(R.id.frameLayout, frag).commit();
                    getSupportActionBar().setTitle("Be an Expert");
                    if(Build.VERSION.SDK_INT>=21){
                        Window window = getWindow();
                        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        window.setStatusBarColor(Color.parseColor(getResources().getString(R.string.color_list_primary_5)));
                    }
                    }

                dl.closeDrawers();

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

I guess there will be no need of putting xml files.

Updated Part :- When i Comment out the code where i am instantiating the Fragment_Navigation And use setUp Method , Then IT works Fine. Here IS code OF Fragment_Navigation Class

package com.qanda;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment_Navigation extends Fragment{
    private DrawerLayout drawer;
    private ActionBarDrawerToggle toggle;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.drawer, container,false);

    return view;
}
void setUp(DrawerLayout dl , Toolbar toolbar ){
    drawer  = dl;
    toggle = new ActionBarDrawerToggle(getActivity(),dl,toolbar,R.string.app_name,R.string.hello_world);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

}
}
Mayank
  • 614
  • 1
  • 5
  • 11
  • Try putting `getWindow()` and everything that has to do with it **before** the `setContentView()`. – Vucko Jul 09 '16 at 11:55
  • @Vucko i have tired to remove that too, but not working – Mayank Jul 09 '16 at 11:58
  • The problem isn't in that class. It's wherever you're using an `ActionBarDrawerToggle`. – Mike M. Jul 09 '16 at 11:59
  • @MikeM. Atually it was working all fine , but then i updated my sdk manager, then all was messed up as i am using eclipse. But now , no compile time errors. If you want i can put code where i am implementing ActionBarDrawerToggle – Mayank Jul 09 '16 at 12:01
  • Nope its not. @Tom – Mayank Jul 09 '16 at 12:15
  • what is the target API? – guipivoto Jul 09 '16 at 12:29
  • Target Api : 21 Min : 11 @GuilhermeP – Mayank Jul 09 '16 at 12:31
  • Thanks.. Sorry for more question... What is the API level of your phone and which is the Support Library version that you are using? – guipivoto Jul 09 '16 at 12:32
  • @GuilhermeP Support Library version 23.2.1 and what do u mean by api level of app – Mayank Jul 09 '16 at 12:36
  • @Mayank Sorry... API Level of your Phone... android version – guipivoto Jul 09 '16 at 12:38
  • @GuilhermeP api version of my phone is 23 – Mayank Jul 09 '16 at 12:39
  • `getActivity()` will return null in `setUp()`. That's nothing to do with your updating SDK manager or anything. Either pass the `Activity` into that method, or initialize the `ActionBarDrawerToggle` somewhere else, like `onCreateView()`, or `onViewCreated()`. Also, that `Fragment` never gets transacted into the `Activity`, so I don't know what you're trying to do there. Does the `ActionBarDrawerToggle` really need to be in a `Fragment`? – Mike M. Jul 09 '16 at 13:05
  • @IsiahMeadows Let's talk again if you have enough reputation and therefore experience about closing as dupe. – Tom Jul 09 '16 at 13:26
  • @Tom The problem here isn't whether the OP understands what an NPE is. – Claudia Jul 09 '16 at 13:28
  • 1
    @IsiahMeadows That's only one half what the dupe explains. But we both shouldn't waste our time in discussing this right now. I suggested a dupe and you (and OP) said it isn't. Now it is up to other users to pass their verdict and either close as dupe as well, or keep it open. We'll see what happens. And btw: my previous comment wasn't meant to be mean to you. Sorry if you thought it were. – Tom Jul 09 '16 at 13:32

0 Answers0