GridView使我的应用程序崩溃-如何解决该问题?

时间:2018-11-09 12:09:57

标签: android gridview

我已经编写了一个应用程序,但是直到现在,没有Grid View的情况都很好,问题是当我在活动主体中声明Grid View时,应用程序在启动时崩溃了。 我在另一个项目和Grid View中编写的相同代码效果很好。

这是我的代码。

活动主xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/colorWhite">

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="@drawable/border"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/search_for"
            style="@style/Widget.AppCompat.EditText"
            android:layout_width="286dp"
            android:layout_height="match_parent"
            android:layout_marginStart="0dp"
            android:layout_marginLeft="0dp"
            android:layout_toLeftOf="@id/myButton"
            android:autofillHints=""
            android:imeOptions="actionSearch"
            android:importantForAutofill="no"
            android:textColor="#000000"
            android:textColorHint="#dbdbdb" />

        <Button
            android:id="@+id/myButton"
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:text="Go" />


    </RelativeLayout>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <GridView
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="90dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" />
</RelativeLayout>
    <!--<WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />-->

</LinearLayout>

网格XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/icons"
        android:layout_width="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:src="@drawable/ic_home_button"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/textView"
        android:gravity="center"
        android:text="A"
        android:layout_below="@+id/icons"
        android:layout_width="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="22dp"
        android:layout_height="wrap_content" />
</RelativeLayout>

主要活动Java

package com.example.zhuniqia.searchwithus;


import android.Manifest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.Toast;

import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {

    public WebView myWeb;
    public Button myBtn;
    public EditText myEdit;
    GridView gridView;

    String letterList[] = {"a","a","a","a","a","a" };
    int lettersIcon[] = {R.drawable.ic_reading_list,R.drawable.ic_reading_list,R.drawable.ic_reading_list,R.drawable.ic_reading_list,R.drawable.ic_reading_list,R.drawable.ic_reading_list,};
    private ArrayList<String> mNames = new ArrayList<>();
    private ArrayList<String> mImageUrls = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    //    myWeb = findViewById(R.id.webView);
        myBtn = findViewById(R.id.myButton);
        myEdit = findViewById(R.id.search_for);

    gridView = (GridView) findViewById(R.id.gridview);

    ImageAdapter adapter = new ImageAdapter(MainActivity.this, lettersIcon, letterList);

    gridView.setAdapter(adapter);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(MainActivity.this, "Clicked Letter:" + letterList[position], Toast.LENGTH_SHORT).show();
        }
    });
        myEdit.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                final String myText = myEdit.getText().toString();
                myWeb.loadUrl("google.com/search?q=" + myText);
                myWeb.getSettings().setJavaScriptEnabled(true);
                myWeb.setWebChromeClient(new WebChromeClient());
                myWeb.setWebViewClient(new WebViewClient());
                return true;
            }
            return false;
        });
            myBtn.setOnClickListener(v -> {
                try {
                    InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                    final String myText = myEdit.getText().toString();

                    myWeb.loadUrl("https://www.bing.com/search?q=" + myText);
                    myWeb.getSettings().setJavaScriptEnabled(true);
                    myWeb.setWebChromeClient(new WebChromeClient());
                    myWeb.setWebViewClient(new WebViewClient());


                } catch (Exception e) {

                }
            });
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION)
                .subscribe(granted -> {
                    if (granted) {
                        Toast.makeText(MainActivity.this,
                                "Location is allowed",
                                Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MainActivity.this,
                                "Denied permission without ask never again",
                                Toast.LENGTH_SHORT).show();
                    }
                });
    getImages();
    }
    private void getImages() {
        mImageUrls.add("https://assets.materialup.com/uploads/7c07872c-0f2c-4f9d-8bb9-e243f44185f0/preview.jpg");
        mNames.add("Bing");





        initRecyclerView();
    }
    private  void initRecyclerView() {

        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(layoutManager);
        RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, mNames, mImageUrls);
        recyclerView.setAdapter(adapter);
    }


}

这是用于Grid View的图像适配器

package com.example.zhuniqia.searchwithus;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private int icons[];
    private String textView[];
    private LayoutInflater inflater;

    public ImageAdapter(Context c, int icons[], String textView[] ) {
    this.mContext = c;
    this.icons = icons;
    this.textView = textView;
    }

    public int getCount() {
        return textView.length;
    }

    public Object getItem(int position) {
        return textView[position];
    }

    public long getItemId(int position) {
        return position;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
       View gridView = convertView;
        if (convertView == null) {
            inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            gridView = inflater.inflate(R.layout.list_grid, null);
            // if it's not recycled, initialize some attributes
        }
        ImageView icon = (ImageView) gridView.findViewById(R.id.icons);
        TextView letter = (TextView) gridView.findViewById(R.id.textView);
        icon.setImageResource(icons[position]);
        letter.setText(textView[position]);
        return gridView;
        }

}

2 个答案:

答案 0 :(得分:1)

我不确定您的ImageView中的可绘制对象,但是确定它是否存在膨胀问题。因此,尝试改用app:srcCompat

<ImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/icons"
    android:layout_width="wrap_content"
    android:layout_marginTop="8dp"
    android:layout_marginLeft="8dp"
    app:srcCompat="@drawable/ic_home_button"
    android:layout_height="wrap_content" />

或者只需删除android:src="@drawable/ic_home_button",因为您总是要在适配器中为其设置图像。

还请再次检查您的ic_home_button可绘制文件,确保它们在正确的位置。如果仅位于drawable-v24中,则将其移至drawable或适当的位置。

否则,请尝试:

而不是从系统服务中获取LayoutInflater
inflater = LayoutInflater.from(parent.getContext());
gridView = inflater.inflate(R.layout.list_grid, parent, false);

答案 1 :(得分:0)

  

它是png(v24)

仅将此图像复制到where TQueue : UniqueQueue<TItem>, new() 文件夹中,而不是drawable
如果您还没有这样做的话。
否则,无法在其他android版本中找到它。