在Android中使用OnClickListiner时显示FC错误

时间:2015-10-13 06:46:24

标签: android android-fragments

我想使用Button来使用Layout,使用OnCLickListener方法显示FC错误。在Fragment中使用此代码。
我的Fragment代码:

 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.newsms_card_fragment, container, false);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.newSMS_recycler);
        context = view.getContext();

        b1 = (Button) view.findViewById(R.id.b2);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        return view;
    }

显示此行的错误:

 b1.setOnClickListener(new View.OnClickListener() {

Logcat错误:

10-13 10:07:22.126  21653-21653/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.tellfa.smsbox, PID: 21653
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
            at com.tellfa.smsbox.fragments.Fragment_newSMS.onCreateView(Fragment_newSMS.java:66)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)
            at android.view.View.measure(View.java:17496)
            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
            at android.view.View.measure(View.java:17496)
            at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:868)
            at android.view.View.measure(View.java:17496)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5466)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
            at android.view.View.measure(View.java:17496)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5466)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1438)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:724)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:615)
            at android.view.View.measure(View.java:17496)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5466)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at android.view.View.measure(View.java:17496)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5466)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1438)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:724)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:615)
            at android.view.View.measure(View.java:17496)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5466)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2636)
            at android.view.View.measure(View.java:17496)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2031)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1193)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1400)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1078)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5875)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5349)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

newsms_card_fragment xml代码:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/newSMS_recycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            android:layout_marginRight="5dp"
            android:layout_marginLeft="5dp"/>

    </LinearLayout>

newsms_card_layout xml code : 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/l1"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/message_bg">

        <RelativeLayout
            android:id="@+id/top_l"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="45dp"
            android:layout_marginTop="5dp">

            <TextView
                android:id="@+id/newSMS_username_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:text="نام کاربری جوووووون"
                android:textColor="#8c8c8c"
                android:textSize="23sp" />

        </RelativeLayout>

        <Button
            android:id="@+id/b2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="0.5dp"
            android:layout_below="@+id/top_l"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:background="#808c8c8c" />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/l2"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_below="@+id/l1"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="35dp"
        android:background="@drawable/message_bg_2"
        android:visibility="gone">

    </RelativeLayout>

    <de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/users_avatar"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_alignParentRight="true"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:scaleType="centerCrop"
        android:src="@drawable/all_users_avatar"
        app:civ_border_color="#8c8c8c"
        app:civ_border_width="1dp" />


    <TextView
        android:id="@+id/newSMS_like_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/newSMS_count_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/newSMS_category_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/newSMS_Date_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/newSMS_hour_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/sms_newsms_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


</RelativeLayout>

你能帮我解决一下吗? tnx all dears&lt; 3

4 个答案:

答案 0 :(得分:1)

在newsms_card_fragment.xml中只有与listview类似的RecyclerView。此布局没有任何按钮并删除此行

b1 = (Button) view.findViewById(R.id.b2);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });

答案 1 :(得分:1)

请实施OnClickListener

 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.newsms_card_fragment, container, false);
    mRecyclerView = (RecyclerView) view.findViewById(R.id.newSMS_recycler);
    context = view.getContext();

    b1 = (Button) view.findViewById(R.id.b2);
    b1.setOnClickListener(this);
    return view;
}
public void onClick(View view) {
 // code 
    }
});

答案 2 :(得分:1)

您的Fragment包含newsms_card_fragment.xml视图,其中只有一个孩子,RecyclerView。 您应该RecyclerViewRecyclerViewAdapter ViewHolder newsms_card_layout RecyclerViewOnClickListener作为xml。 然后在<input type="hidden" id="hiddenValue" ng-value="hiddenValue" /> 的适配器中,您可以为按钮.service('user', function () { this.user = ''; this.setUser = function(name) { this.user = name; } }); 实现代码。

有关详情,请参阅如何开始RecyclerView

答案 3 :(得分:1)

问题是你在一个尚不存在的视图上调用findViewById。您需要扩充所需的布局,然后可以调用findViewById。请尝试以下代码。

View b2Layout = getLayoutInflater().inflate(R.layout.<b2s layout name>, null);
Button b1 = (Button)b2Layout.findViewById(R.id.b2);