03-01 17:00:28.629:E / AndroidRuntime(30201):致命异常:主要

时间:2013-03-01 15:16:27

标签: java android gridview

使用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)

3 个答案:

答案 0 :(得分:2)

堆栈跟踪说明了这一点:

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

v.getId()会返回id的{​​{1}},这与View中的int不同。这意味着你打电话给R.drawable,它就会失败。

可能有用:

v.getResources().getDrawable()

但是,由于imageView = (ImageView)v; image=imageView.getDrawable(); view.setImageDrawable(image); 似乎位于第一个xml中,而image位于另一个xml布局文件中,因此最好将GridView传递给包含xml的活动position然后从那里获得Drawable。例如

image

然后在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); } LogoActivity

onCreate()

答案 1 :(得分:0)

通过阅读您的代码,我只能猜出您的错误发生的位置,但似乎您在这些行中获得了NullPointerException

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

您的XML布局文件不包含ID为GridView的{​​{1}},因此gridview返回null。在此之后,您尝试调用findViewById(R.id.gridview),它本身很可能会调用异常。

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

答案 2 :(得分:0)

这似乎是个问题。您的GridView位于另一个xml布局中。因此,使用R.id.gridview引用它可以正常工作,但在它上面附加onClick将无法正常工作,因为它无法在活动的当前XML布局中找到。尝试在此XML中移动gridView - (R.layout.activity_list_logos)