Как кодировать для Grid Layout onClickListerner - PullRequest
0 голосов
/ 20 октября 2018

Во время кодирования для 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

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

frag_home.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>

AndroidManifest.xml

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".OnBoardActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity android:name=".Registration">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity android:name=".Login">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity android:name=".fragment_quotes" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity android:name=".Stories" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity android:name=".Feedback">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

Ответы [ 2 ]

0 голосов
/ 20 октября 2018

Переместите ваши коды формы 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);
}
0 голосов
/ 20 октября 2018

Попробуйте переместить любой код, который ссылается на ваши представления, на onViewCreated, а не onCreateView.В частности, эта часть:

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

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