【问题标题】:How to code for Grid Layout onClickListerner如何为网格布局 onClickListener 编写代码
【发布时间】:2018-10-20 18:30:04
【问题描述】:

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

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

【问题讨论】:

    标签: java android gridview onclick onclicklistener


    【解决方案1】:

    尝试将任何引用您的视图的代码移至 onViewCreated,而不是 onCreateView。具体来说,这部分:

    mainGrid = (GridLayout)getActivity().findViewById(R.id.mainGrid);
    
        setSingleEvent0(mainGrid);
        setSingleEvent1(mainGrid);
        setSingleEvent2(mainGrid);
        setSingleEvent3(mainGrid);
        setSingleEvent4(mainGrid);
        setSingleEvent5(mainGrid);
    

    【讨论】:

    • 我不明白。请你详细说明。!!
    • 您的代码位于 onCreateView 方法中,该方法在您的视图创建之前运行 - 因此尝试访问它们将引发异常。您必须重写 onViewCreated 方法并将您的代码放在那里,因为该方法在视图创建后运行。希望这更清楚
    • 我已经这样做了,但是之后日志中的屏幕上出现错误闪烁:Process: com.example.lenovo.skanda, PID: 8794 android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.lenovo.skanda/com.example.lenovo.skanda.ChatFragment};您是否在 AndroidManifest.xml 中声明了此活动?
    • @itzPrateek 正如错误所说,您应该在清单中定义您的活动。见stackoverflow.com/questions/19122386/…
    • @itzPrateek 而不是调用 (GridLayout)getActivity().findViewById(R.id.mainGrid) 尝试在 onViewCreated() 方法中调用 (GridLayout)view.findViewById(R.id.mainGrid) .其中 view 是一个输入参数。您的网格视图是片段的一部分,而不是宿主活动。
    【解决方案2】:

    将您的代码从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);
    }
    

    【讨论】:

    • 在实现这个之后......它不适用于片段类。它仅适用于 AppCompatActivity。
    • 好的,但是在网格布局上点击卡片视图后,日志中会弹出一个错误。错误 :: Process: com.example.lenovo.skanda, PID: 9819 android.content.ActivityNotFoundException: Unable to find explicit Activity class {com.example.lenovo.skanda/com.example.lenovo.skanda.PositivityFragment};您是否在 AndroidManifest.xml 中声明了此活动?
    猜你喜欢
    • 2021-09-04
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多