在类名称为变量的地方启动Intent?

问题描述

| 我正在尝试从表示意图名称
nIntent
的字符串变量访问意图。 问题:如果将变量
nIntent
替换为
Querydisplay.class
名称,则可以正确触发,但是如果我使用变量,为什么会得到典型的
No Activity found to handle Intent { act=Querydisplay.class (has extras) }
? 我不明白,因为它可以很好地进行硬编码(在清单中)。有任何想法吗? 谢谢你的帮助。
lv.setonItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0,View v,int arg2,long arg3) {

                String url = \"\";
                url = (String) v.getTag();

                int nI = c.getColumnIndexOrThrow(\"intent\");
                String nIntent = c.getString(nI);

                int tvTitle = c.getColumnIndexOrThrow(\"title\");
                String title = c.getString(tvTitle);

                int tvLabel = c.getColumnIndexOrThrow(\"label\");
                String label = c.getString(tvLabel);

                String queryKey = \"SELECT * FROM \" + label + \" ORDER BY `_id` ASC\";

                //Intent i = new Intent(nIntent);<- This doesn\'t?? work
                Intent i = new Intent(this,Querydisplay.class);<- This works
                i.putExtra(\"QUERY_KEY\",queryKey);
                i.putExtra(\"url\",url);
                i.putExtra(\"TITLE\",title);
                i.putExtra(\"LABEL\",label);
                Querydisplay.this.startActivity(i);
            }
        });
更新:这是正确的代码
lv.setonItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0,long arg3) {

                String url = \"\";
                url = (String) v.getTag();

                int nI = c.getColumnIndexOrThrow(\"intent\");
                String intent = c.getString(nI);
                Class<?> nIntent = null;
                try {
                    nIntent = Class.forName(intent);
                } catch (ClassNotFoundException e) {
                    // Todo Auto-generated catch block
                    e.printstacktrace();
                }

                int tvTitle = c.getColumnIndexOrThrow(\"title\");
                String title = c.getString(tvTitle);

                int tvLabel = c.getColumnIndexOrThrow(\"label\");
                String label = c.getString(tvLabel);

                String queryKey = \"SELECT * FROM \" + label + \" ORDER BY `_id` ASC\";

                Intent i = new Intent(Querydisplay.this,nIntent);
                i.putExtra(\"QUERY_KEY\",label);
                Querydisplay.this.startActivity(i);
            }
        });
    

解决方法

        问题在于“ 0”是一个字符串,而不是“ 7”对象。如果您查看Intent文档,则会看到唯​​一接受单个String的构造函数要求该字符串表示一个动作,而不是一个类。 如果您要将String转换为Class,我认为您可以使用Class.forName方法来实现。     ,        在两种情况下,无论您使用的是硬编码的Queryclass名称还是nIntent,Intent调用都应该相同。 可以在意图调用中使用[Class] .this而不是使用\'this \'。 对于db中的动态类,只需在字符串中返回classname,而不是将整个intent名称作为字符串返回即可,因为创建Intent时需要传递上下文。仍然您会遇到问题。。。恢复问题。干杯!