如何为Grid Layout onClickListerner编码

时间:2018-10-20 18:30:04

标签: java android gridview onclick onclicklistener

在对Grid Layout onClickListerner进行编码时。

的日志报告中出现错误

原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ android.view.View android.view.ViewGroup.getChildAt(int)”         在com.example.lenovo.skanda.HomeFragment.setSingleEvent0(HomeFragment.java:42)         在com.example.lenovo.skanda.HomeFragment.onCreateView(HomeFragment.java:31)

HomeFragment.java

[18-10-20 11:18:17:743 PDT] [[TIMESTAMP, EVENT], [Mon Oct 01 05:10:13 GMT+05:30 2018, Event 1], [Mon Oct 01 05:10:16 GMT+05:30 2018, Event 2], [Mon Oct 01 05:10:17 GMT+05:30 2018, Event 3], [Mon Oct 01 05:10:24 GMT+05:30 2018, Event 4], [Mon Oct 01 05:10:26 GMT+05:30 2018, Event 5], [Mon Oct 01 05:10:32 GMT+05:30 2018, Event 6], [Mon Oct 01 05:10:38 GMT+05:30 2018, Event 7], [Mon Oct 01 05:10:38 GMT+05:30 2018, Event 8], [Mon Oct 01 05:10:45 GMT+05:30 2018, Event 9], [Mon Oct 01 05:11:07 GMT+05:30 2018, Event 10]]

fragment_home.xml

package com.example.lenovo.skanda;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class HomeFragment extends Fragment {

    GridLayout mainGrid;
    View viewroot;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        viewroot = inflater.inflate(R.layout.fragment_home,container,false);

        mainGrid = (GridLayout)getActivity().findViewById(R.id.mainGrid);

        setSingleEvent0(mainGrid);
        setSingleEvent1(mainGrid);
        setSingleEvent2(mainGrid);
        setSingleEvent3(mainGrid);
        setSingleEvent4(mainGrid);
        setSingleEvent5(mainGrid);

        return viewroot;
    }

    private void setSingleEvent0(GridLayout mainGrid) {
        CardView cardView = (CardView)mainGrid.getChildAt(0);
        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "Its CEO", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void setSingleEvent1(GridLayout mainGrid) {
        CardView cardView = (CardView)mainGrid.getChildAt(1);
        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "Its CTO", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void setSingleEvent2(GridLayout mainGrid) {
        CardView cardView = (CardView)mainGrid.getChildAt(2);
        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "Its Accounts", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void setSingleEvent3(GridLayout mainGrid) {
        CardView cardView = (CardView)mainGrid.getChildAt(3);
        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "Its Receptionist", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void setSingleEvent4(GridLayout mainGrid) {
        CardView cardView = (CardView)mainGrid.getChildAt(4);
        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "Its Chief Architecture", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void setSingleEvent5(GridLayout mainGrid) {
        CardView cardView = (CardView)mainGrid.getChildAt(5);
        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "Its Chief Accountant", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:weightSum="10"
    android:background="#fdbbed">

    <RelativeLayout
        android:layout_weight="3"
        android:background="#FFF"
        android:layout_width="match_parent"
        android:layout_height="0dp">

        <TextView
            android:id="@+id/textGrid"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:textStyle="bold"
            android:padding="11dp"
            android:text="“Believe in yourself! Have faith in your abilities! Without a humble but reasonable confidence in your own powers you cannot be successful or happy.”"
            android:textColor="@android:color/black"
            android:textSize="15dp" />

    </RelativeLayout>

    <GridLayout
        android:background="@drawable/back"
        android:columnCount="2"
        android:rowCount="3"
        android:alignmentMode="alignMargins"
        android:padding="15dp"
        android:layout_weight="7"
        android:id="@+id/mainGrid"
        android:layout_width="match_parent"
        android:layout_height="200dp">

        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:background="#affa86"
            app:cardElevation="12dp"
            app:cardCornerRadius="48dp"
            android:id="@+id/cardview1"
            >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                >

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="center_horizontal"
                    android:id="@+id/symptoms_image"
                    android:src="@drawable/sucide1"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="SUCIDE AND CAUSES"
                    android:textAlignment="center"
                    android:textColor="@android:color/black"
                    android:textSize="10sp"
                    android:paddingTop="5dp"
                    android:textStyle="bold" />


            </LinearLayout>

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

        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:background="#85f557"
            app:cardElevation="8dp"
            app:cardCornerRadius="48dp"
            >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                >

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="center_horizontal"
                    android:id="@+id/Stories_image"
                    android:src="@drawable/script"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="SURVIVOR TALES"
                    android:textAlignment="center"
                    android:textColor="@android:color/black"
                    android:textSize="10sp"
                    android:paddingTop="5dp"
                    android:textStyle="bold" />


            </LinearLayout>

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

        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:background="#111"
            app:cardElevation="8dp"
            app:cardCornerRadius="48dp"
            >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                >

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="center_horizontal"
                    android:id="@+id/Positivity_image"
                    android:src="@drawable/meditation"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="POSITIVITY AND MOTIVATION"
                    android:textAlignment="center"
                    android:textColor="@android:color/black"
                    android:textSize="10sp"
                    android:paddingTop="5dp"
                    android:textStyle="bold" />


            </LinearLayout>

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

        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:background="#111"
            app:cardElevation="8dp"
            app:cardCornerRadius="48dp"
            >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                >

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="center_horizontal"
                    android:id="@+id/Chatwithus_image"
                    android:scaleType="fitCenter"
                    android:src="@drawable/quotes"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="QUOTES"
                    android:textAlignment="center"
                    android:textColor="@android:color/black"
                    android:textSize="10sp"
                    android:paddingTop="5dp"
                    android:textStyle="bold" />


            </LinearLayout>

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

        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:background="#111"
            app:cardElevation="8dp"
            app:cardCornerRadius="48dp"
            >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                >

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="center_horizontal"
                    android:id="@+id/Faq_image"
                    android:src="@drawable/ic_chat1"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="CHAT WITH US"
                    android:textAlignment="center"
                    android:textColor="@android:color/black"
                    android:textSize="10sp"
                    android:paddingTop="5dp"
                    android:textStyle="bold" />


            </LinearLayout>

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

        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:background="#111"
            app:cardElevation="8dp"
            app:cardCornerRadius="48dp"
            >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical"
                >

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="center_horizontal"
                    android:id="@+id/Aboutus_image"
                    android:src="@drawable/ic_aboutus"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="ABOUT US"
                    android:textAlignment="center"
                    android:textColor="@android:color/black"
                    android:textSize="10sp"
                    android:paddingTop="5dp"
                    android:textStyle="bold" />

            </LinearLayout>

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

    </GridLayout>

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>

2 个答案:

答案 0 :(得分:0)

尝试将所有引用视图的代码移到onViewCreated而不是onCreateView上。具体来说,这部分:

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    /* content */
   /div>
</nav>

答案 1 :(得分:0)

将您的代码从onCreateView移到onViewCreated

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    mainGrid = (GridLayout) view.findViewById(R.id.mainGrid);
    setSingleEvent0(mainGrid);
    setSingleEvent1(mainGrid);
    setSingleEvent2(mainGrid);
    setSingleEvent3(mainGrid);
    setSingleEvent4(mainGrid);
    setSingleEvent5(mainGrid);
}