-1

I am trying to implement a setOnClickListener method on CardView but I keep getting a null exception whenever I switch orientation of device (works fine if I start by either portrait or landscape mode):

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.navajoinc.navajoinc.navfeedbackapp/com.navajoinc.navajoinc.navfeedbackapp.feedBackCardView}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

My Java Class is:

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;
    }
}

Activity XML:

<?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>

I have tried many solutions on Stackoverflow and other websites but none of it has worked ? Could you please help with this issue?

Shivam Kumar
  • 1,892
  • 2
  • 21
  • 33
Zeusox
  • 7,708
  • 10
  • 31
  • 60
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – ADM Jun 02 '18 at 03:18
  • @IssmeilB. Can you create two different layouts of activity_feed_back_card_view with portrait and landscape mode – Shivam Kumar Jun 02 '18 at 04:24
  • I already have two different layouts ! It's just weird how it's working on one and not the other ! – Zeusox Jun 03 '18 at 16:10

1 Answers1

1

I've tried the code and it's working. Even after rotation. However, I changed the implementation of the onclick methods to simply show a toast rather that launch an intent since you didn't provide the classes. I also ommited all the code related to the menu.

Also, why are you setting the clickable=true property on your card view if you are setting an onClickListener within your code? It's redundant. Also, I don't see any reason to assert clickable=false in your <LinearLayout> tags.

I don't think you are providing enough information to locate the error, or it's related to the classes that haven't been provided. Is the app crashing simply on rotation, or after you try clicking on something? Are you sure the crash is related to rotation, and not an attempt to launch a new activity?

CodeNovice
  • 343
  • 1
  • 11