MainActivity在android studio中抛出空指针异常

时间:2016-05-07 07:54:36

标签: java android nullpointerexception

MainActivity

com.example.vivek.settleup;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Toolbar toolbar;
DrawerLayout drawerLayout;
private static final String log = "myLogg";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    toolbar.setTitle("Home");
    getSupportActionBar().setTitle("Home");
    toolbar.setNavigationIcon(R.drawable.ic_action_name);
    toolbar.setNavigationContentDescription("Navigation Icon");
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    toolbar.setNavigationOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                Toast.makeText(MainActivity.this,"touched",Toast.LENGTH_LONG).show();
            }
        }
     );

    NavigationDrawerFragment1 drawerFragment = (NavigationDrawerFragment1) getSupportFragmentManager().
            findFragmentById(R.id.fragment);
            drawerFragment.setUp(drawerLayout,toolbar); /**this call is throwing nullpointer exception**/

  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.main,menu);
      return super.onCreateOptionsMenu(menu);
  }
}

NavigationDrawerFragment1

package com.example.vivek.settleup;
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;


/**
 * A simple {@link Fragment} subclass.
 */
public class NavigationDrawerFragment1 extends Fragment {
    DrawerLayout mDrawerLayout;
    ActionBarDrawerToggle mDrawerToggle;
    private static final String log = "myLog";

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_navigation_drawer_fragment1, container, false);
    }

    public void setUp(DrawerLayout drawerLayout, Toolbar toolbar) {
        mDrawerLayout = drawerLayout;
        mDrawerToggle = new ActionBarDrawerToggle(getActivity(),drawerLayout,toolbar,
                R.string.drawer_open,R.string.drawer_close){

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
        };
        mDrawerLayout.addDrawerListener(mDrawerToggle);
    }    
}

0 个答案:

没有答案