-3

I have wrote an app but till now without Grid view works very good the problem is that when I have declared the Grid View into the activity main now the app crashed at launch. The same code I have wrote in another project and Grid View works very good.

This is my code.

activity main 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>

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

Main Activity 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);
    }


}

This is the Image Adapter for 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;
        }

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
TheCoderGuy
  • 771
  • 6
  • 24
  • 51

2 Answers2

1

I'm not sure about your drawable in your ImageView, but it sure has problem inflating it. So try using app:srcCompat instead:

<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" />

Or just remove android:src="@drawable/ic_home_button" since you're always going to set image to it in your adapter.

Also please check your ic_home_button drawable files again, make sure they are in the right place. If it's locate only in drawable-v24, then move it out to drawable or somewhere appropriate.

Otherwise, instead of getting LayoutInflater from the system service, try:

inflater = LayoutInflater.from(parent.getContext());
gridView = inflater.inflate(R.layout.list_grid, parent, false);
Aaron
  • 3,764
  • 2
  • 8
  • 25
0

it is a png (v24)

Copy this image in drawable folder not drawable-v24 only
if you have not already done so.
Otherwise it can't be found for other android versions.

forpas
  • 160,666
  • 10
  • 38
  • 76