I’m trying to access an intent from a string variable that represents the intent’s name, nIntent.
Question: If I replace the variable nIntent with the QueryDisplay.class name, it fires correctly, but if I use a variable, why do I get the typical No Activity found to handle Intent { act=QueryDisplay.class (has extras) }?
I don’t get it because it works fine hard-coded (it’s in the manifest). Any ideas?
Thanks for your help.
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);
}
});
UPDATE: Here is the correct code:
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 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", queryKey);
i.putExtra("url", url);
i.putExtra("TITLE", title);
i.putExtra("LABEL", label);
QueryDisplay.this.startActivity(i);
}
});
The problem is that
nIntentis a String, not aClassobject. If you look at the Intent documentation, you’ll see that the only constructor that accepts a single String requires that the string represents an action, not a class.If you want to convert a String to a Class, I think you can do so with the Class.forName method.