想知道我的应用程序崩溃的原因

时间:2014-04-24 13:41:50

标签: java android android-intent casting

我刚接触到android。这个应用程序是教程的一部分。现在我想知道为什么如果我点击gridview的项目会崩溃。我已经阅读了日志,但不知道错误的演员阵容在哪里。

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)

3 个答案:

答案 0 :(得分:4)

看看你的代码,你似乎在这里写了一个拼写错误:

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

应该是

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

或此TextView的id名称。

除此之外,您还需要学习从LogCat中读取消息,因为它们通常非常有用。 Cleary它说:

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

表示原因和

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

表示代码中出现的位置。

理解如何在开始时阅读堆栈跟踪可能会很痛苦,但是当您获得该堆栈跟踪时,它将节省大量时间,因为您将能够确定应用程序崩溃的原因和位置。 / p>

答案 1 :(得分:0)

您的问题在logcat中解释:

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

通过查看Logcat的下一行,您可以看到它发生的位置:

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

所以改变以下内容:

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

进入这个:

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

答案 2 :(得分:0)

下面的错误: -

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

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

您将ImageView转换为textview。