首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >CardView NullPointerException onClickListener

CardView NullPointerException onClickListener
EN

Stack Overflow用户
提问于 2018-06-02 06:28:36
回答 1查看 526关注 0票数 -1

我试图在CardView上实现一个setOnClickListener方法,但每当我切换设备的方向时,我都会得到一个空异常(如果我以纵向或横向模式启动,效果都很好):

虚方法:无法启动activity ComponentInfo{com.navajoinc.navajoinc.navfeedbackapp/com.navajoinc.navajoinc.navfeedbackapp.feedBackCardView}:java.lang.NullPointerException:尝试在空对象引用上调用虚方法的空方法

我的Java类是:

代码语言:javascript
复制
public class feedBackCardView extends FragmentActivity {
    CardView neutralCustomer;
    CardView happyCustomer;

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

        neutralCustomer =  findViewById(R.id.neutralCustomer);
        happyCustomer =  findViewById(R.id.happyCard);

        neutralCustomer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {    
                Intent intent = new Intent(feedBackCardView.this, thank_you_positive.class);
                startActivity(intent);
            }
        });

        happyCustomer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {    
                Intent intent = new Intent(feedBackCardView.this, thank_you_positive.class);
                startActivity(intent);
            }
        });    
    }

    public void unsatisfiedCutomerSelectionMethod(View v){
        singleChoiceClass unsatisfied_customer_dialog = new singleChoiceClass();    
        unsatisfied_customer_dialog.show(getFragmentManager(), "unsatisfied_customer_dialog");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the main_menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {    
        switch(item.getItemId()) {
            case R.id.contactus:    
                Intent callIntent = new Intent(Intent.ACTION_DIAL);
                callIntent.setData(Uri.parse("tel:800.525.5097"));
                startActivity(callIntent);    
                break;
            case R.id.visitsite:
                Intent siteIntent = new Intent(Intent.ACTION_VIEW);
                siteIntent.setData(Uri.parse("http://navajoinc.com"));
                startActivity(siteIntent);   
                break;
            default:
                return super.onOptionsItemSelected(item);
        }   
        return true;
    }
}

活动XML:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".feedBackCardView"
    android:padding="10dp"
    android:background="@color/colorWhiteSmoke"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:fillViewport="true"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="center">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14dp"
                android:layout_gravity="center"
                android:text="Please tap a response :"
                android:textColor="@color/colorLightGrey" />

            <LinearLayout

                android:clipToPadding="false"
                android:gravity="center"
                android:clickable="false"
                android:layout_gravity="center"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <android.support.v7.widget.CardView
                    android:id="@+id/happyCard"
                    android:foreground="?android:selectableItemBackground"
                    android:clickable="true"
                    android:layout_width="160dp"
                    android:layout_height="190dp"
                    android:layout_margin="10dp"
                    app:cardElevation="3dp"
                   app:cardBackgroundColor="@color/cardview_light_background"
                    app:cardCornerRadius="3dp"
                    >

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical"
                        android:background="@color/colorWhite"
                        android:gravity="center">
                        <ImageView
                            android:layout_width="64dp"
                            android:layout_height="64dp"
                            android:background="@drawable/happy_face"
                            android:padding="10dp"/>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textStyle="bold"
                            android:layout_marginTop="10dp"
                            android:textColor="@color/colorLightGrey"
                            android:text="Happy Customer"/>
                        <View
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                        android:background="@color/cardview_light_background"
                            android:layout_margin="2dp"/>
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:gravity="center"
                            android:textSize="11dp"
                            android:textColor="@color/colorTextGrey"
                            android:text="I have had a happy experience"/>


                    </LinearLayout>

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

            </LinearLayout>

            <LinearLayout
                android:id="@+id/neutralLayout"
                android:clipToPadding="false"
                android:gravity="center"
                android:orientation="horizontal"
                android:clickable="false"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <android.support.v7.widget.CardView
                    android:id="@+id/neutralCustomer"
                    android:foreground="?android:selectableItemBackground"
                    android:clickable="true"
                    android:layout_width="160dp"
                    android:layout_height="190dp"
                    android:layout_margin="10dp"
                    app:cardElevation="3dp"
                    app:cardBackgroundColor="@color/cardview_light_background"
                    app:cardCornerRadius="3dp"
                    >

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical"
                        android:background="@color/colorWhite"
                        android:gravity="center">
                        <ImageView
                            android:layout_width="64dp"
                            android:layout_height="64dp"
                            android:background="@drawable/neutral_face"
                            android:padding="10dp"/>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textStyle="bold"
                            android:layout_marginTop="10dp"
                            android:textColor="@color/colorLightGrey"
                            android:text="Neutral Customer"/>
                        <View
                            android:layout_width="match_parent"
                            android:layout_height="1dp"

    android:background="@color/cardview_light_background"
                            android:layout_margin="2dp"/>
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:gravity="center"
                            android:textSize="11dp"
                            android:textColor="@color/colorTextGrey"
                            android:text="My experience was neutral"/>


                    </LinearLayout>

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

            </LinearLayout>

            <LinearLayout
                android:clipToPadding="false"
                android:gravity="center"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <android.support.v7.widget.CardView

                    android:foreground="?android:selectableItemBackground"
                    android:clickable="true"
                    android:onClick="unsatisfiedCutomerSelectionMethod"
                    android:layout_width="160dp"
                    android:layout_height="190dp"
                    android:layout_margin="10dp"
                    app:cardElevation="3dp"
                    app:cardBackgroundColor="@color/cardview_light_background"
                    app:cardCornerRadius="3dp"
                    >

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical"
                        android:background="@color/colorWhite"
                        android:gravity="center">
                        <ImageView
                            android:layout_width="64dp"
                            android:layout_height="64dp"
                            android:background="@drawable/angry_face"
                            android:padding="10dp"/>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textStyle="bold"
                            android:layout_marginTop="10dp"
                            android:textColor="@color/colorLightGrey"
                            android:text="Unsatisfied Customer"/>
                        <View
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                            android:background="@color/cardview_light_background"
                            android:layout_margin="2dp"/>
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:gravity="center"
                            android:textSize="11dp"
                            android:textColor="@color/colorTextGrey"
                            android:text="My experience was unpleasant"/>

                    </LinearLayout>    
                </android.support.v7.widget.CardView>    
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

我在Stackoverflow和其他网站上尝试了许多解决方案,但都没有奏效?你能帮我解决这个问题吗?

EN

回答 1

Stack Overflow用户

发布于 2018-06-02 07:14:48

我试过这段代码,它正在工作。即使在旋转之后。但是,我更改了onclick方法的实现,只显示一个toast,而不是启动一个intent,因为您没有提供这些类。我还省略了所有与菜单相关的代码。

另外,如果要在代码中设置clickable=true,为什么还要在卡片视图上设置onClickListener属性?这是多余的。此外,我认为没有任何理由在您的<LinearLayout>标记中断言clickable=false

我认为你没有提供足够的信息来定位错误,或者它与没有提供的类有关。应用程序是简单地在旋转时崩溃,还是在你尝试点击后崩溃?您确定崩溃与旋转有关,而不是试图启动新活动吗?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50651723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档