android中Fragment和toolbar?
澳门银河官网
澳门银河官网
当前位置 : 澳门银河官网 > 澳门银河官网开户

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.setTitlejava.lang.CharSequence on a null object reference
                                                 at android.app.ActivityThread.performLaunchActivityActivityThread.java:2416
                                                 at android.app.ActivityThread.handleLaunchActivityActivityThread.java:2476
                                                 at android.app.ActivityThread.-wrap11ActivityThread.java
                                                 at android.app.ActivityThread$H.handleMessageActivityThread.java:1344
                                                 at android.os.Handler.dispatchMessageHandler.java:102
                                                 at android.os.Looper.loopLooper.java:148
                                                 at android.app.ActivityThread.mainActivityThread.java:5417
                                                 at java.lang.reflect.Method.invokeNative Method
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.runZygoteInit.java:726
                                                 at com.android.internal.os.ZygoteInit.mainZygoteInit.java:616
                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method void android.support.v7.widget.Toolbar.setTitlejava.lang.CharSequence on a null object reference
                                                 at com.example.fiz.bottomtabbar.MainActivity.onCreateMainActivity.java:31
                                                 at android.app.Activity.performCreateActivity.java:6237
                                                 at android.app.Instrumentation.callActivityOnCreateInstrumentation.java:1107
                                                 at android.app.ActivityThread.performLaunchActivityActivityThread.java:2369
                                                 at android.app.ActivityThread.handleLaunchActivityActivityThread.java:2476 
                                                 at android.app.ActivityThread.-wrap11ActivityThread.java 
                                                 at android.app.ActivityThread$H.handleMessageActivityThread.java:1344 
                                                 at android.os.Handler.dispatchMessageHandler.java:102 
                                                 at android.os.Looper.loopLooper.java:148 
                                                 at android.app.ActivityThread.mainActivityThread.java:5417 
                                                 at java.lang.reflect.Method.invokeNative 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 onCreateBundle savedInstanceState {
    super.onCreatesavedInstanceState;
    Toolbar toolbar = Toolbar findViewByIdR.id.toolbar;
    toolbar.setTitle"demo";
    setSupportActionBartoolbar;
    setContentViewR.layout.activity_main;

// initView;

}

private void initView {
    ll_top = LinearLayout findViewByIdR.id.ll_top;
    mPager = ViewPager findViewByIdR.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.addactivityfragment;/**首页**/
    fragmentsList.addgroupFragment;
    fragmentsList.addfriendsFragment;
    fragmentsList.addchatFragment;

    mPager.setAdapternew MyFragmentPagerAdapter
            getSupportFragmentManager, fragmentsList;
    mPager.setCurrentItem0;

    BottomTabBar navigationBar = new BottomTabBarthis;
    ll_top.setBackgroundColorColor.parseColor"#F8F8FF";
    navigationBar.attachToParentll_top, new String[]{"首页", "消息", "发现",
            "我"}, mPager;
}

@Override
public boolean onCreateOptionsMenuMenu menu {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater.inflateR.menu.main, menu;
    return true;
}

@Override
public boolean onOptionsItemSelectedMenuItem 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.onOptionsItemSelecteditem;
}

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

    private ArrayList<Fragment> fragmentsList;

    public MyFragmentPagerAdapterFragmentManager fm {
        superfm;
    }

    public MyFragmentPagerAdapterFragmentManager fm,
                                  ArrayList<Fragment> fragments {
        superfm;
        this.fragmentsList = fragments;
    }

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

    @Override
    public Fragment getItemint arg0 {
        return fragmentsList.getarg0;
    }

    @Override
    public int getItemPositionObject object {
        return super.getItemPositionobject;
    }

}

}

换下顺序:

setContentViewR.layout.activity_main;
Toolbar toolbar = Toolbar findViewByIdR.id.toolbar;
    toolbar.setTitle"demo";
    setSupportActionBartoolbar;

这个你看看布局文件里面 初始化toolbar的ID对应的是不是正确
看log 像是toolbar没正确初始化

广告位

澳门银河官网