如何删除空指针异常错误.getSupportActionBar

时间:2016-02-20 03:38:56

标签: android nullpointerexception fragment

我尝试了很多解决方案,但他们没有工作..你能帮我解决..

我认为错误是由于getActivity()中的null值,但我已经尝试了每个解决方案来删除它,可能是我错过了一些东西..

这就是事情

    public class StuffMainFragment extends Fragment {
private ViewPager mPager;
private SlidingTabsLayout mTabs;

private OnFragmentInteractionListener mListener;
private LayoutInflater mMenuInflater;
public AppCompatActivity mCustomgetSupport;
public StuffMainFragment() {
    // Required empty public constructor
}




@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

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


    Toolbar toolbar = (Toolbar)view. findViewById(R.id.app_bar);

    final ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();

    if (actionBar!=null) {
        ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
        actionBar.setDisplayShowHomeEnabled(true);
    }
    NavigationDrawerFragment drawerFragment=(NavigationDrawerFragment)
            (getActivity()).getSupportFragmentManager().findFragmentById(R.id.nav_frag);
    drawerFragment.setUp(R.id.nav_frag, (DrawerLayout)view.findViewById(R.id.drawer_layout),toolbar);

    mPager=(ViewPager)view.findViewById(R.id.pager);
    mPager.setAdapter(new MyPagerAdapter((getActivity()).getSupportFragmentManager()));
    mTabs=(SlidingTabsLayout)view.findViewById(R.id.tabs);
    mTabs.setDistributeEvenly(true);

    mTabs.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
    mTabs.setSelectedIndicatorColors(getResources().getColor(R.color.colorAccent));
    mTabs.setViewPager(mPager);


    return view;
}
public interface OnFragmentInteractionListener {
    void onLogout();
}

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

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    switch (id) {
        case R.id.action_logout:
            Log.d("PK", "LOGOUT Menu Item Clicked!");
            mListener.onLogout();
            return true;
    }
    return false;
}



class MyPagerAdapter  extends FragmentPagerAdapter {

    String[] tabs;
    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
        tabs=getResources().getStringArray(R.array.tabs);
    }

    @Override
    public Fragment getItem(int position) {
        MyFragment myFrllagment=MyFragment.getInsance(position);
        return myFrllagment;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabs[position];
    }

    @Override
    public int getCount() {
        return 6;
    }
}
public static class MyFragment extends Fragment{
    private TextView mTextView;
    public static MyFragment getInsance(int position){
        MyFragment myfragment=new MyFragment();
        Bundle args=new Bundle();
        args.putInt("position",position);
        myfragment.setArguments(args);
        return myfragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState){
        View layout=inflater.inflate(R.layout.fragment_my,container,false);
        mTextView=(TextView)layout.findViewById(R.id.position);
        Bundle bundle=getArguments();
        if(bundle!=null)
        {
            mTextView.setText("The Page Selected Is"+bundle.getInt("position"));
        }
        return layout;
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}



}

N错误是

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rana.sahaj.myyu/com.rana.sahaj.myyu.MainActivity}: java.lang.NullPointerException
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2200)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249)
                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:141)
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                     at android.os.Looper.loop(Looper.java:136)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5052)
                                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                                     at java.lang.reflect.Method.invoke(Method.java:515)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
                                                                     at dalvik.system.NativeStart.main(Native Method)
                                                                  Caused by: java.lang.NullPointerException
                                                                     at com.rana.sahaj.myyu.StuffMainFragment.onCreateView(StuffMainFragment.java:69)
                                                                     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
                                                                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
                                                                     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                                                                     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
                                                                     at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330)
                                                                     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
                                                                     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
                                                                     at android.app.Activity.performStart(Activity.java:5258)
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249) 
                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:141) 
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212) 
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                     at android.os.Looper.loop(Looper.java:136) 
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5052) 
                                                                     at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                     at java.lang.reflect.Method.invoke(Method.java:515) 

fragment_stuff_main.xml

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:id="@+id/stuffID"
         tools:context="com.rana.sahaj.myyu.StuffMainFragment">

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="match_parent"

    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.rana.sahaj.myyu.MainActivity">




    <android.support.v4.widget.DrawerLayout android:layout_width="match_parent"
                                            android:layout_height="match_parent"
                                            android:id="@+id/drawer_layout"
                                            xmlns:app="http://schemas.android.com/apk/res-auto"
                                            tools:context="com.rana.sahaj.myyu.MainActivity">
        <LinearLayout

            android:layout_below="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#263238"
            android:orientation="vertical">


            <include layout="@layout/app_bar"
                     android:id="@+id/app_bar"/>

            <com.rana.sahaj.myyu.tabs.SlidingTabsLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/tabs"
                app:tabGravity="fill"
                app:tabMode="scrollable"
                app:tabSelectedTextColor="@color/colorAccent"
                app:theme="@style/MyCustomThemeToolbar"/>
            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>


        </LinearLayout>
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin">


        </RelativeLayout>
        <fragment
            android:id="@+id/nav_frag"
            android:layout_width="@dimen/nav_drawer_width"
            android:layout_height="match_parent"
            android:name="com.rana.sahaj.myyu.NavigationDrawerFragment"
            android:layout_gravity="start"
            app:layout="@layout/fragment_navigation_drawer"
            tools:layout="@layout/fragment_navigation_drawer"/>

    </android.support.v4.widget.DrawerLayout>








</android.support.v4.widget.DrawerLayout>

强文

包含的tablayout(我已经包含在内)..

    <?xml version="1.0" encoding="utf-8"?>

   <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"

                               android:paddingTop="@dimen/app_bar_top_padding"
                               android:layout_width="match_parent"
                               android:layout_height="wrap_content"                                            xmlns:app="http://schemas.android.com/apk/res-auto"
                android:background="#1A237E"
                               app:theme="@style/MyCustomThemeToolbar"
                               app:popupTheme="@style/MyCustomOverlay" >

   </android.support.v7.widget.Toolbar>

0 个答案:

没有答案