1

所以使用gridview创建了视图。我有一种包含图像视图的布局。单击 gridview 上的视图时,我想激活相同的布局,但在布局中使用单击的图像。但是,我遇到了错误。

这是我的布局

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

这是我的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;
}

}

这是我的图像适配器代码

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,

};
}

如果有人帮助我,我将非常感激。

这是我的错误信息

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)
4

3 回答 3

2

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);
}
于 2013-03-02T20:39:23.513 回答
0

通过阅读您的代码,我只能猜测您的错误发生在哪里,但似乎您NullPointerException在以下几行中得到了 a:

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

您的 XML-layout-file 不包含GridView带有 ID的 a,gridview因此findViewById(R.id.gridview)返回 null。在那之后,您尝试调用setAdapter(...)它本身最有可能调用异常。

如果您向我们提供有关异常的更多信息,将会非常有帮助。

于 2013-03-01T20:33:50.617 回答
0

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)

于 2013-03-02T20:38:48.943 回答