尝试在空对象引用上调用虚拟方法'int android.view.View.getVisibility()'

时间:2018-11-22 07:45:29

标签: java android

lang.NullPointerException:尝试在空对象引用上调用虚拟方法'int android.view.View.getVisibility()'

我不知道如何解决

我的课:

public class SimpleCommodityActivity extends BaseActivity {

    @BindView(R.id.view_pager_tab)
    SmartTabLayout view_pager_tab;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.img_back)
    ImageView img_back;
    @BindView(R.id.img_cart)
    ImageView img_cart;
    @BindView(R.id.txt_badge_shopping_cart)
    TextView txt_badge_shopping_cart;
    @BindView(R.id.txt_title_commodity)
    TextView txt_title_commodity;
    @BindView(R.id.layout_loading)
    LinearLayout layout_loading;
    @BindView(R.id.layout_error)
    LinearLayout layout_error;
    private String commodityId;
    private boolean shouldExecuteOnResume;

    @Override
    protected int getViewId() {
        return R.layout.activity_simple_commodity;
    }

    @Override
    protected void initializeActivity() {
        Intent intent = getIntent();
        commodityId = intent.getStringExtra("id");
        Log.e("id", commodityId);
        getData();
        getBadge();
        shouldExecuteOnResume = false;
        txt_title_commodity.setSelected(true);
    }

    @Override
    protected void setupListeners() {

    }

    @Override
    protected void isNetwork(boolean isNetwork) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (shouldExecuteOnResume) {
            getData();
            getBadge();
        } else {
            shouldExecuteOnResume = true;
        }
    }

    private void getData() {
        layout_loading.setVisibility(View.VISIBLE);
        new PakhshiStore().getCommodity(commodityId).subscribe(new DisposableObserver<MainRes<CommodityRes>>() {
            @Override
            public void onNext(MainRes<CommodityRes> value) {
                if (value.getResponseType() != 1) {
                    showMessage(value.getError());
                } else {
                    CommodityRes commodityRes = value.getData();
                    if (commodityRes != null) {
                        //datas = commodityRes;
                        AppStore.setCommodityData(commodityRes);
                        setData(commodityRes);
                        //scrollview.setVisibility(View.VISIBLE);
                    } else {
                        layout_loading.setVisibility(View.GONE);
                        //scrollview.setVisibility(View.GONE);
                        layout_error.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                layout_loading.setVisibility(View.GONE);
                //scrollview.setVisibility(View.GONE);
                layout_error.setVisibility(View.VISIBLE);
            }

            @Override
            public void onComplete() {
                layout_loading.setVisibility(View.GONE);
            }
        });
    }

    private void setData(CommodityRes commodityRes) {
        txt_title_commodity.setText(commodityRes.getTitle());
        FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
                getSupportFragmentManager(), FragmentPagerItems.with(this)
                .add(R.string.buy_commodity, SimpleCommodityFrag.class)
                .add(R.string.ommoditi_info, SimpleCommodityFrag.class)
                .create());
        viewpager.setAdapter(adapter);
        view_pager_tab.setViewPager(viewpager);
    }

    public void getBadge() {
        if (AppStore.getToken() != null) {
            new PakhshiStore().getCartCount().subscribe(new DisposableObserver<MainRes<Integer>>() {
                @Override
                public void onNext(MainRes<Integer> value) {
                    if (value.getResponseType() != 1) {
                        showMessage(value.getError());
                    } else {
                        if (value.getData() != 0) {
                            txt_badge_shopping_cart.setVisibility(View.VISIBLE);
                            txt_badge_shopping_cart.setText(String.valueOf(value.getData()));
                        } else {
                            txt_badge_shopping_cart.setVisibility(View.GONE);
                        }
                    }
                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onComplete() {

                }
            });
        } else {
            txt_badge_shopping_cart.setVisibility(View.GONE);
        }
    }

    public void initSpinnerSize(final int position) {
        if (AppStore.getCommodityData().getPackageTiers().size() != 0) {
            Bundle bundle = new Bundle();
            bundle.putInt("position",position);
            FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
                    getSupportFragmentManager(), FragmentPagerItems.with(this)
                    .add(R.string.buy_commodity, SimpleCommodityFrag.class,bundle)
                    .add(R.string.ommoditi_info, SimpleCommodityFrag.class,bundle)
                    .create());
            viewpager.setAdapter(adapter);
            view_pager_tab.setViewPager(viewpager);
        }
    }
}

我的用户界面:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/layout_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:layoutDirection="rtl"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/layout_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:background="@color/colorPrimary">

        <ImageView
            android:id="@+id/img_back"
            android:layout_width="@dimen/_40sdp"
            android:layout_height="@dimen/_40sdp"
            android:layout_alignParentRight="true"
            android:padding="@dimen/_15sdp"
            android:rotation="180"
            android:src="@drawable/ic_back"
            android:tint="@color/white" />


        <ImageView
            android:id="@+id/img_cart"
            android:layout_width="@dimen/_40sdp"
            android:layout_height="@dimen/_40sdp"
            android:layout_alignParentLeft="true"
            android:padding="@dimen/_10sdp"
            android:src="@drawable/ic_buy"
            android:tint="@color/white" />

        <TextView
            android:id="@+id/txt_badge_shopping_cart"
            android:layout_width="@dimen/_15sdp"
            android:layout_height="@dimen/_15sdp"
            android:layout_alignRight="@id/img_cart"
            android:layout_marginTop="@dimen/_5sdp"
            android:background="@drawable/red_circle"
            android:gravity="center"
            android:text="1"
            android:textColor="@color/white"
            android:textSize="@dimen/font_size_xxsmall" />

        <ImageView
            android:id="@+id/img_share"
            android:layout_width="@dimen/_40sdp"
            android:layout_height="@dimen/_40sdp"
            android:layout_toRightOf="@id/img_cart"
            android:padding="@dimen/_10sdp"
            android:src="@drawable/ic_share"
            android:tint="@color/white"
            android:visibility="gone" />

        <TextView
            android:id="@+id/txt_title_commodity"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/_40sdp"
            android:layout_marginLeft="@dimen/_3sdp"
            android:layout_marginRight="@dimen/_3sdp"
            android:layout_toLeftOf="@id/img_back"
            android:layout_toRightOf="@id/img_cart"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:gravity="center"
            android:marqueeRepeatLimit="marquee_forever"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:textColor="@color/white"
            android:textSize="@dimen/font_size_small" />
    </RelativeLayout>

    <com.ogaclejapan.smarttablayout.SmartTabLayout
        android:id="@+id/view_pager_tab"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_40sdp"
        android:background="@color/colorAccent"
        app:stl_clickable="true"
        app:stl_customTabTextLayoutId="@layout/custom_tab"
        app:stl_customTabTextViewId="@id/txt"
        app:stl_defaultTabBackground="?attr/selectableItemBackground"
        app:stl_defaultTabTextAllCaps="true"
        app:stl_defaultTabTextColor="@color/colorAccent"
        app:stl_defaultTabTextHorizontalPadding="16dp"
        app:stl_defaultTabTextMinWidth="0dp"
        app:stl_defaultTabTextSize="@dimen/font_size_xlarge"
        app:stl_distributeEvenly="false"
        app:stl_dividerColor="@color/transparent"
        app:stl_dividerThickness="@dimen/_1sdp"
        app:stl_drawDecorationAfterTab="false"
        app:stl_indicatorColor="@color/black_30"
        app:stl_indicatorCornerRadius="@dimen/_18sdp"
        app:stl_indicatorGravity="center"
        app:stl_indicatorInterpolation="smart"
        app:stl_indicatorThickness="@dimen/_30sdp"
        app:stl_titleOffset="24dp"
        app:stl_underlineThickness="1dp"  />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/layout_title" />

    <LinearLayout
        android:id="@+id/layout_loading"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background_color"
        android:gravity="center"
        android:orientation="vertical"
        android:visibility="gone">

        <ImageView
            android:layout_width="@dimen/_100sdp"
            android:layout_height="@dimen/_100sdp"
            android:alpha="0.3"
            android:src="@drawable/logo" />

        <com.github.ybq.android.spinkit.SpinKitView
            style="@style/SpinKitView.Large.ThreeBounce"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            app:SpinKit_Color="@color/gray_hint" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout_error"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background_color"
        android:gravity="center"
        android:orientation="vertical"
        android:visibility="gone">

        <ImageView
            android:layout_width="@dimen/_80sdp"
            android:layout_height="@dimen/_80sdp"
            android:alpha="0.5"
            android:src="@drawable/ic_warning" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_10sdp"
            android:gravity="center"
            android:text="@string/error_commodity_not_found"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="@dimen/font_size_normal" />

    </LinearLayout>
</RelativeLayout>

错误:

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'int android.view.View.getVisibility()'

我真的不知道这是什么错误 我删除片段适配器,再次显示此错误

日志:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.pakhshi.app.pakhshi, PID: 28599
    java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getVisibility()' on a null object reference
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1623)
        at android.view.View.measure(View.java:23297)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
        at android.view.View.measure(View.java:23297)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6928)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)
        at android.view.View.measure(View.java:23297)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6928)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
        at android.view.View.measure(View.java:23297)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6928)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:23297)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6928)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
        at android.view.View.measure(View.java:23297)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6928)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:899)
        at android.view.View.measure(View.java:23297)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2837)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1874)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2129)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1743)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7773)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
        at android.view.Choreographer.doCallbacks(Choreographer.java:723)
        at android.view.Choreographer.doFrame(Choreographer.java:658)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6949)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

0 个答案:

没有答案