android中Fragment和toolbar?

做了一个demo就是下面是菜单,上面的toobar在调试过称出现空指针,
03-18 07:35:50.567 1908-1908/? E/AndroidRuntime: FATAL EXCEPTION: main

                                             Process: com.example.fiz.bottomtabbar, PID: 1908
                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fiz.bottomtabbar/com.example.fiz.bottomtabbar.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.Toolbar.setTitle(java.lang.CharSequence)' on a null object reference
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                 at android.os.Looper.loop(Looper.java:148)
                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.Toolbar.setTitle(java.lang.CharSequence)' on a null object reference
                                                 at com.example.fiz.bottomtabbar.MainActivity.onCreate(MainActivity.java:31)
                                                 at android.app.Activity.performCreate(Activity.java:6237)
                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                 at android.os.Looper.loop(Looper.java:148) 
                                                 at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                 at java.lang.reflect.Method.invoke(Native Method) 想问问怎么修改代码,新手不会了?这***


package com.example.fiz.bottomtabbar;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.support.v7.widget.Toolbar;

public class MainActivity extends AppCompatActivity {

private ViewPager mPager;
private ArrayList<Fragment> fragmentsList;
private LinearLayout ll_top;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("demo");
    setSupportActionBar(toolbar);
    setContentView(R.layout.activity_main);

// initView();

}

private void initView() {
    ll_top = (LinearLayout) findViewById(R.id.ll_top);
    mPager = (ViewPager) findViewById(R.id.vPager);
    fragmentsList = new ArrayList<Fragment>();

    Fragment activityfragment = TestFragment.newInstance("界面一");
    Fragment groupFragment = TestFragment.newInstance("界面2");
    Fragment friendsFragment = TestFragment.newInstance("界面3");
    Fragment chatFragment = TestFragment.newInstance("界面4");

    Fragment about = About.about("1234");
    Fragment homepage = HomePage.homePage("1234");
    fragmentsList.add(activityfragment);/**首页**/
    fragmentsList.add(groupFragment);
    fragmentsList.add(friendsFragment);
    fragmentsList.add(chatFragment);

    mPager.setAdapter(new MyFragmentPagerAdapter(
            getSupportFragmentManager(), fragmentsList));
    mPager.setCurrentItem(0);

    BottomTabBar navigationBar = new BottomTabBar(this);
    ll_top.setBackgroundColor(Color.parseColor("#F8F8FF"));
    navigationBar.attachToParent(ll_top, new String[]{"首页", "消息", "发现",
            "我"}, mPager);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, 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);
}

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

    private ArrayList<Fragment> fragmentsList;

    public MyFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    public MyFragmentPagerAdapter(FragmentManager fm,
                                  ArrayList<Fragment> fragments) {
        super(fm);
        this.fragmentsList = fragments;
    }

    @Override
    public int getCount() {
        return fragmentsList.size();
    }

    @Override
    public Fragment getItem(int arg0) {
        return fragmentsList.get(arg0);
    }

    @Override
    public int getItemPosition(Object object) {
        return super.getItemPosition(object);
    }

}

}

阅读 2.8k
评论
    2 个回答

    换下顺序:

    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("demo");
        setSupportActionBar(toolbar);
    
      相似问题
      推荐文章