Domanda

im new to android. This app is part of a tutorial. Now i'm wondering why this crashes if I click on an item of the gridview. I've read the log but dont know where the wrong cast is.

MainActivity.java

public class MainActivity extends Activity implements OnItemClickListener {

    GridView myGrid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myGrid=(GridView) findViewById(R.id.gridView);
        myGrid.setAdapter(new CustomAdapter(this));
        myGrid.setOnItemClickListener(this);
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
        Intent intent=new Intent(this, Dialog.class);
        ViewHolder holder=(ViewHolder) view.getTag();
        AppName temp=(AppName) holder.AppImage.getTag();
        intent.putExtra("appImage", temp.image);
        intent.putExtra("appName", temp.appname);
        startActivity(intent);
    }

}
class AppName{
int image;
String appname;
AppName(int image, String appname){
    this.image=image;
    this.appname=appname;
    }
}

class ViewHolder{
ImageView AppImage;
ViewHolder(View v){
    AppImage=(ImageView) v.findViewById(R.id.imageView1);
    }
}

class CustomAdapter extends BaseAdapter{

    ArrayList<AppName> list;
    Context context;
    public CustomAdapter(Context c) {
        this.context=c;
        list= new ArrayList<AppName>();
        Resources res=c.getResources();
            String[] Appnames=res.getStringArray(R.array.Appnames);
        int[]    images=R.drawable.aim,R.drawable.amazon,R.drawable.amazon2,R.drawable.android,R.drawable.aol,R.drawable.apple,R.drawable.appstore,R.drawable.bebo,R.drawable.behance,R.drawable.bing,R.drawable.bleetbox,R.drawable.blinklist,R.drawable.blogger,R.drawable.brightkite1,R.drawable.brightkite2,R.drawable.cargocollective,R.drawable.coroflot,R.drawable.delicious,R.drawable.designfloat,R.drawable.designmoo};

        for(int i=0;i<20;i++){
            list.add(new AppName(images[i], Appnames[i]));
        }

    }
    @Override
    public int getCount() {
    return list.size();
    }

    @Override
    public Object getItem(int i) {
    return list.get(i);
    }

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

    @Override
    public View getView(int i, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        View row=convertView;
        if(row==null){
        LayoutInflater inflater=(LayoutInflater)  context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row=inflater.inflate(R.layout.single_item, parent, false);
        holder=new ViewHolder(row);
        row.setTag(holder);
        }
                else{
         holder=(ViewHolder) row.getTag();
        }

        AppName temp=list.get(i);

        holder.AppImage.setImageResource(temp.image);
        holder.AppImage.setTag(temp);
        return row;
    }

}

Dialog.java

public class Dialog extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dialog);
    Intent intent=getIntent();
    if(intent!=null){
        int imageId=intent.getIntExtra("appImage", R.drawable.aim);
        String appName=intent.getStringExtra("appName");
        ImageView DialogImage=(ImageView) findViewById(R.id.imageViewDialog);
        DialogImage.setImageResource(imageId);
        TextView DialogText=(TextView) findViewById(R.id.imageViewDialog);
        DialogText.setText("This Icon belongs to "+appName);
        }
    }
    public void Close(View v) {
        finish();
    }

}

LogCat

04-24 06:37:26.149: E/AndroidRuntime(2607): FATAL EXCEPTION: main
04-24 06:37:26.149: E/AndroidRuntime(2607): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nando.gridview/com.nando.gridview.Dialog}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView
04-24 06:37:26.149: E/AndroidRuntime(2607):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
04-24 06:37:26.149: E/AndroidRuntime(2607):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
04-24 06:37:26.149: E/AndroidRuntime(2607):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-24 06:37:26.149: E/AndroidRuntime(2607):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
04-24 06:37:26.149: E/AndroidRuntime(2607):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 06:37:26.149: E/AndroidRuntime(2607):     at android.os.Looper.loop(Looper.java:137)
04-24 06:37:26.149: E/AndroidRuntime(2607):     at android.app.ActivityThread.main(ActivityThread.java:5103)
04-24 06:37:26.149: E/AndroidRuntime(2607):     at java.lang.reflect.Method.invokeNative(Native Method)
04-24 06:37:26.149: E/AndroidRuntime(2607):     at java.lang.reflect.Method.invoke(Method.java:525)
04-24 06:37:26.149: E/AndroidRuntime(2607):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-24 06:37:26.149: E/AndroidRuntime(2607):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-24 06:37:26.149: E/AndroidRuntime(2607):     at dalvik.system.NativeStart.main(Native Method)
04-24 06:37:26.149: E/AndroidRuntime(2607): Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView
04-24 06:37:26.149: E/AndroidRuntime(2607):     at com.nando.gridview.Dialog.onCreate(Dialog.java:21)
04-24 06:37:26.149: E/AndroidRuntime(2607):     at android.app.Activity.performCreate(Activity.java:5133)
04-24 06:37:26.149: E/AndroidRuntime(2607):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-24 06:37:26.149: E/AndroidRuntime(2607):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
È stato utile?

Soluzione

Looking at your code it seems that you made a typo here:

TextView DialogText=(TextView) findViewById(R.id.imageViewDialog);

It should be

TextView DialogText=(TextView) findViewById(R.id.textViewDialog);

or whatever the name of the id for this TextView is.

Apart from that, you need to learn to read the messages from the LogCat as they are often very informative. Cleary it says:

Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView 

which indicates the reason and

at com.nando.gridview.Dialog.onCreate(Dialog.java:21)

indicates where it occurs in your code.

It might be a pain to understand how to read the stacktrace at the beginning but when you'll get that, it will save a lot of time as you will be able to identify why and where the app crashes.

Altri suggerimenti

You problem is explained in the logcat :

android.widget.ImageView cannot be cast to android.widget.TextView

And by looking at the next line of your Logcat you can see where it happened:

at com.nando.gridview.Dialog.onCreate(Dialog.java:21)

So change the following:

TextView DialogText=(TextView) findViewById(R.id.imageViewDialog);

Into this:

TextView DialogText=(TextView) findViewById(R.id.idofyourtextview);

Mistake on below line :-

    ImageView DialogImage=(ImageView) findViewById(R.id.imageViewDialog);
    DialogImage.setImageResource(imageId);

or

    TextView DialogText=(TextView) findViewById(R.id.imageViewDialog);
    DialogText.setText("This Icon belongs to "+appName);

you converting ImageView to textview.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top