GridView в студии Android останавливает приложение

Я пробовал много примеров о представлении сетки, но будет отображаться пустая активность или приложение остановлено Я пытался использовать все решения на сайте до того, как написал свою проблему, но безрезультатно. Код появляется без ошибок

Кто-нибудь может мне помочь

Основная деятельность

package com.safaa.user.grideview;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

public class MainActivity extends AppCompatActivity {
GridView gridView;
int images[] = {
        R.drawable.image1,R.drawable.image2,
        R.drawable.image3,R.drawable.image4,
        R.drawable.image5,R.drawable.image6,
        R.drawable.image7,R.drawable.image8,
        R.drawable.image9};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gridView=findViewById(R.id.simpleGridView);
        CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), images);
        gridView.setAdapter(customAdapter);

      gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(MainActivity.this, Second_Activity.class);
                intent.putExtra("image", images[position]); // put image data in Intent
                startActivity(intent); // start Intent
            }
        });



    }
}

Activity_main.xml

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



      <GridView

            android:id = "@+id/simpleGridView"
            android:layout_width = "fill_parent"
            android:layout_height = "fill_parent"
            android:columnWidth = "90dp"
            android:gravity = "center"
            android:horizontalSpacing = "10dp"
            android:numColumns = "auto_fit"
            android:stretchMode = "columnWidth"
            android:verticalSpacing = "10dp" />

    </LinearLayout>

CustomAdapter.java

package com.safaa.user.grideview;

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

public class CustomAdapter extends BaseAdapter {

    Context context;
    int images[];
    LayoutInflater inflter;
    public CustomAdapter(Context applicationContext, int[] images) {
        this.context = applicationContext;
        this.images = images;
        inflter = (LayoutInflater.from(applicationContext));
    }

    @Override
    public int getCount() {
        return images.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = inflter.inflate(R.layout.activity_gridview, null); // inflate the layout
        ImageView icon = (ImageView) view.findViewById(R.id.icon); // get the reference of ImageView
        icon.setImageResource(images[i]); // set logo images
        return view;    }
}

activity_gridView

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




    <ImageView
        android:id = "@+id/icon"
        android:layout_width = "match_parent"
        android:layout_height = "120dp"
        android:scaleType = "fitXY"
        android:layout_gravity = "center_horizontal"
        android:src = "@drawable/image1" />
</LinearLayout>

Second_Activity

package com.safaa.user.grideview;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

public class Second_Activity extends AppCompatActivity {
    ImageView selectedImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_);
        selectedImage = (ImageView) findViewById(R.id.selectedImage); // init a ImageView
        Intent intent = getIntent(); // get Intent which we set from Previous Activity
        selectedImage.setImageResource(intent.getIntExtra("image", 0)); // get image from Intent and set it in ImageView
    }
    }

second_Activity.xml

<?xml version = "1.0" encoding = "utf-8"?>
<RelativeLayout  xmlns:android = "http://schemas.android.com/apk/res/android"
    xmlns:app = "http://schemas.android.com/apk/res-auto"
    xmlns:tools = "http://schemas.android.com/tools"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent"
    android:background = "#fff"
    >
    <ImageView
        android:id = "@+id/selectedImage"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:layout_centerInParent = "true"
        android:scaleType = "fitXY" />
</RelativeLayout >

Mainfast

<?xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns:android = "http://schemas.android.com/apk/res/android"
    package = "com.safaa.user.grideview">

    <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 = ".MainActivity">
            <intent-filter>
                <action android:name = "android.intent.action.MAIN" />

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

</manifest>

buildGradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.safaa.user.grideview"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

}

Пожалуйста, укажите свои попытки в вопросе вместо публикации ссылки на учебный сайт

Andreas 28.11.2018 07:00

я сделал это спасибо за заметку

Safaa Omar 28.11.2018 13:49
0
2
40
0

Другие вопросы по теме