无效android.support.v4.app.Fragment.setMenuVisibility(布尔)'上一个空对象引用 [英] void android.support.v4.app.Fragment.setMenuVisibility(boolean)' on a null object reference

查看:4144
本文介绍了无效android.support.v4.app.Fragment.setMenuVisibility(布尔)'上一个空对象引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误才开始后,我开始在项目中使用的片段出现,是因为我想给的JSONObject,

The error started to appear only after I started using fragments in the project, because I want to give Jsonobject,

这是我的code ..

here is my code..

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;
private ViewPager mPager;
private SlidingTabLayout mTabs;
private MyPagerAdapter adapter;

public static final int product_result = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    setSupportActionBar(toolbar);
    assert getSupportActionBar() != null;
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    NavigationDrawerFragment navigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.fragment_nav_drawer);
    navigationDrawerFragment.setUp(R.id.fragment_nav_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);

    mPager = (ViewPager) findViewById(R.id.pager);
    adapter = new MyPagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(adapter);

    mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
    mTabs.setDistributeEvenly(true);
    mTabs.setCustomTabView(R.layout.custom_tab_view, R.id.tabText);
    int bgColor = ContextCompat.getColor(this, R.color.colorPrimary);
    mTabs.setBackgroundColor(bgColor);
    mTabs.setSelectedIndicatorColors(ContextCompat.getColor(MainActivity.this, R.color.colorAccent));
    mTabs.invalidate();
    mTabs.setViewPager(mPager);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {

        return true;
    }
    if (id == R.id.navigate) {
        startActivity(new Intent(this, SubActivity.class));
    }
    if (id == R.id.TabActivity) {
        startActivity(new Intent(this, ActivityUsingTabLibrary.class));
    }
    if (id == R.id.VectorTest) {
        startActivity(new Intent(this, VectorTestActivity.class));
    }
    return super.onOptionsItemSelected(item);
}


class MyPagerAdapter extends FragmentStatePagerAdapter {
    int icons[] = {R.drawable.home, R.drawable.hot_article, R.drawable.dizzy_person};

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

    @Override
    public Fragment getItem(int position) {

        Fragment fragment = null;
        switch(position){
            case product_result:
                fragment = FragmentProduct.newInstance("","");
                break;
        }
        return fragment;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Drawable drawable = ResourcesCompat.getDrawable(getResources(), icons[position], null);
        drawable.setBounds(0, 0, 36, 36);
        ImageSpan imageSpan = new ImageSpan(drawable);
        SpannableString spannableString = new SpannableString(" ");
        spannableString.setSpan(imageSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    @Override
    public int getCount() {
        return 3;
    }
}

}

这是我的fragment.class,其中I M传递的JSONObject敬酒。

this is my fragment.class where i m passing jsonObject toast.

public class FragmentProduct extends Fragment {

public static final String product_url = "http://oasisgroups.com/oApp/product.php";

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

private String mParam1;
private String mParam2;
private VolleySingleton volleySingleton;
private ImageLoader imageLoader;
private RequestQueue requestQueue;

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

public static FragmentProduct newInstance(String param1, String param2) {
    FragmentProduct fragment = new FragmentProduct();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

public static String getRequestUrl(int limit) {
    return product_url + "&limit=" + limit;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }

    volleySingleton = VolleySingleton.getInstance();
    requestQueue = volleySingleton.getmRequestQueue();
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, getRequestUrl(10),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    L.t(getActivity(), response.toString());
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    requestQueue.add(request);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_product, container, false);
}

}

堆栈跟踪

02-29 12:10:01.047 17100-17100/com.mydesign.rockstar.materialdesignexample E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mydesign.rockstar.materialdesignexample, PID: 17100
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setMenuVisibility(boolean)' on a null object reference
    at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:116)
    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:870)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1086)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
    at android.view.View.measure(View.java:18804)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:901)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
    at android.view.View.measure(View.java:18804)
    at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:940)
    at android.view.View.measure(View.java:18804)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
    at android.view.View.measure(View.java:18804)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
    at android.view.View.measure(View.java:18804)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at android.view.View.measure(View.java:18804)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
    at android.view.View.measure(View.java:18804)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
    at android.view.View.measure(View.java:18804)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2112)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1228)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1464)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6060)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
    at android.view.Choreographer.doCallbacks(Choreographer.java:670)
    at android.view.Choreographer.doFrame(Choreographer.java:606)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
    at android.os.Handler.handleCallback(Handler.java:746)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5443)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

我也看到了这个同样的问题<一href=\"http://stackoverflow.com/questions/30452180/android-attempt-to-invoke-virtual-method-void-android-support-v4-app-fragment\">link我申请了解决方案,但它不会为我工作。我还在原地停留,帮助我。
所以检查我的code和建议我在哪里,我错过了什么。

i also seen this same problem in this link and i apply his solution but it wont work for me.. i m still stuck, help me. so check my code and suggest me where what i missing.

推荐答案

哦,是的..我终于找到了我的错误..首先出现在我的code没有错。一切是正确的。

oh yes.. i finally found my mistake.. first of all there is no mistake in my code. everything is right.

我有三个选项卡的布局而我会只传递一个tablayout和另外两个越来越空,所以我必须要使用3种开关case语句。

i have three tab layout and i m passing only for one tablayout and other two are getting null, so i must have to apply three switch case statement.

 public Fragment getItem(int position) {

    Fragment fragment = null;
    switch(position){
        case product_result:
            fragment = FragmentProduct.newInstance("","");
            break;
    }
    return fragment;
}

而不是

  public Fragment getItem(int num) {
        Fragment fragment = null;
        switch (num) {
            case TAB_PRODUCT_RESULT:
                fragment = FragmentProduct.newInstance("", "");
                break;
            case TAB_SECOND:
                fragment = MyFragment.getInstance(num);
                break;
            case TAB_THIRD:
                fragment = MyFragment.getInstance(num);
                break;
        }
        return fragment;
    }

完蛋了..

这篇关于无效android.support.v4.app.Fragment.setMenuVisibility(布尔)'上一个空对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆