ocry
ocry

Reputation: 53

03-01 17:00:28.629: E/AndroidRuntime(30201): FATAL EXCEPTION: main

So created views by using gridview. I have one layout that includes image view. When views on gridview clicked, I want to activate same layout but with the clicked image in layout. However, I got errors.

Here is my layout

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

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button1"
    android:layout_alignBottom="@+id/button1"
    android:layout_alignParentRight="true"
    android:text="Hints" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="92dp"
    android:src="@drawable/adese" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="69dp"
    android:text="Get Hint" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/imageView1"
    android:layout_alignParentLeft="true"
    android:text="Back" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="32dp"
    android:ems="10" >

    <requestFocus />
</EditText>

</RelativeLayout>

Here is my java

package com.example.turkishlogoquiz;

import android.os.Bundle;
import android.provider.MediaStore.Images;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;

import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;

public class LogoSelectionActivity extends Activity {

ImageView view;
Drawable image; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_logos);

    view = (ImageView)findViewById(R.id.imageView1);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long     id) {

            v = (ImageView)v;
            image=v.getResources().getDrawable(v.getId());
            view.setImageDrawable(image);
            Intent intent = new Intent(LogoSelectionActivity.this,     LogoActivity.class);
            startActivity(intent);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

And here is my Image Adapter code

package com.example.turkishlogoquiz;

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

public class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(Context c) {
    mContext = c;
}

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

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

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

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private Integer[] mThumbIds = {
        R.drawable.adese, R.drawable.birvar,
        R.drawable.agaoglu, R.drawable.akinsoft,
        R.drawable.afra,

};
}

If anyone help me I am very grateful.

Here is my error messages

03-02 22:16:03.524: E/AndroidRuntime(32407): FATAL EXCEPTION: main
03-02 22:16:03.524: E/AndroidRuntime(32407): android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
03-02 22:16:03.524: E/AndroidRuntime(32407):    at android.content.res.Resources.getValue(Resources.java:1026)
03-02 22:16:03.524: E/AndroidRuntime(32407):    at android.content.res.Resources.getDrawable(Resources.java:671)
03-02 22:16:03.524: E/AndroidRuntime(32407):    at com.example.turkishlogoquiz.LogoSelectionActivity$1.onItemClick(LogoSelectionActivity.java:35)
03-02 22:16:03.524: E/AndroidRuntime(32407):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
03-02 22:16:03.524: E/AndroidRuntime(32407):    at android.widget.AbsListView.performItemClick(AbsListView.java:1283)
03-02 22:16:03.524: E/AndroidRuntime(32407):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3074)
03-02 22:16:03.524: E/AndroidRuntime(32407):    at android.widget.AbsListView$1.run(AbsListView.java:4147)
03-02 22:16:03.524: E/AndroidRuntime(32407):    at android.os.Handler.handleCallback(Handler.java:615)
03-02 22:16:03.524: E/AndroidRuntime(32407):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-02 22:16:03.524: E/AndroidRuntime(32407):    at android.os.Looper.loop(Looper.java:137)
03-02 22:16:03.524: E/AndroidRuntime(32407):    at android.app.ActivityThread.main(ActivityThread.java:4898)
03-02 22:16:03.524: E/AndroidRuntime(32407):    at java.lang.reflect.Method.invokeNative(Native Method)
03-02 22:16:03.524: E/AndroidRuntime(32407):    at java.lang.reflect.Method.invoke(Method.java:511)
03-02 22:16:03.524: E/AndroidRuntime(32407):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
03-02 22:16:03.524: E/AndroidRuntime(32407):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
03-02 22:16:03.524: E/AndroidRuntime(32407):    at dalvik.system.NativeStart.main(Native Method)

Upvotes: 1

Views: 287

Answers (3)

A--C
A--C

Reputation: 36449

The stack trace says this:

android.content.res.Resources$NotFoundException: Resource ID #0xffffffff

v.getId() returns the id of the View, which is different than the ints in R.drawable. This means whan you call v.getResources().getDrawable(), it will fail.

What might work:

imageView = (ImageView)v;
image=imageView.getDrawable();
view.setImageDrawable(image);

However, since image seems to be in the first xml, and the GridView in another xml layout file, you are better off passing position to the Activity that contains the xml with image then getting the Drawable from there. Eg

public void onItemClick(AdapterView<?> parent, View v, int position, long  id) {
           Intent intent = new Intent(LogoSelectionActivity.this, LogoActivity.class);
           intent.putExtra ("clicked_position", position)
           startActivity(intent);
       }

Then in LogoActivity in onCreate():

ImageView view = (ImageView)findViewById(R.id.imageView1);

switch (getIntent().getIntExtra ("clicked_position", -1))
{
  case 0:
    view.setImageResouce (R.drawable.x);
    break;
  case 1:
     view.setImageResouce (R.drawable.y);
     break;
  default:
     view.setImageResouce (R.drawable.default);
}

Upvotes: 2

Darpan
Darpan

Reputation: 5795

This seems to be the issue. Your GridView is in another xml-layout. So referring it with R.id.gridview works fine but attaching a onClick on it won't work as it couldn't be found in current XML layout of the activity. Try movind your gridView in this XML - (R.layout.activity_list_logos)

Upvotes: 0

Gerrit-K
Gerrit-K

Reputation: 1308

From reading your code I can only guess where your error occurs but it seems that you're getting a NullPointerException in these lines:

GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));

Your XML-layout-file doesn't contain a GridView with the ID gridview so findViewById(R.id.gridview) returns null. Right after that you try to call setAdapter(...) which itself will most likely call the Exception.

It would be very helpful if you provide us with more information about the Exception.

Upvotes: 0

Related Questions