Android : Not able to start activity which is in another package (from intent)

on Saturday, November 1, 2014


I've got an activity where I can add bookmarks to a favourites list. Each favourite has got a button. When I click that button I have to go to that specific activity. But it shows me an error. The app doesn't crash, but the bookmarked activity isn't started from the click.


Here is the code for the "Favourites" activity:



public class FavouritesActivity extends Activity {

private TextView mEmptyText;
private LinearLayout mBookmarkLayout;

private Context mContext;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favourites_layout);

mContext = this;

mEmptyText = (TextView) findViewById(R.id.empty_textview);
mBookmarkLayout = (LinearLayout) findViewById(R.id.bookmark_insert_point);

getAllKeys();

}

private void getAllKeys()
{
SharedPreferences sp = this.getSharedPreferences("favs", MODE_PRIVATE);
Map<String,?> keys = sp.getAll();

int count = 0;
for(Map.Entry<String,?> entry : keys.entrySet())
{
String value = entry.getValue().toString();
System.out.println("!!!!!!!!!!!!!!!!!value = "+value);
String delimiter = ",";
String[] values_array = value.split(delimiter);
addBookmark(values_array);
count++; //keep track of the number of bookmarks
}

//if there are no bookmarks, display a text view saying so. Otherwise, make the text view go away
if (count == 0)
{
mEmptyText.setVisibility(View.VISIBLE);
mEmptyText.setText(getString(R.string.no_favs));
}
else
mEmptyText.setVisibility(View.GONE);

}

private void addBookmark(String[] values_array)
{
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.fav_individual, null);

TextView text = (TextView) v.findViewById(R.id.bookmark_text);
text.setSelected(true);
ImageButton button = (ImageButton) v.findViewById(R.id.bookmark_button);
v.getContext();

text.setText(values_array[1]);

final String myClass = values_array[0];
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Class<?> cl = null;
try {
cl = Class.forName(myClass);
Intent myIntent = new Intent(mContext, cl);
startActivity(myIntent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});

// insert into main view
mBookmarkLayout.addView(v, 0, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
System.out.println("!!!!!!!!!!!!!!!!!!!!Just added a view");
}

}


The error is the onClick method, exactly in this line:



cl = Class.forName(myClass);


It gives me a ClassNotFoundException.


It seems that with this code I'm able to start an activity which is inside this package. But the problem is that the activity I want to start is inside a different package. The logcat says that it coulnd't find the activity in the current package.


So how can I open an activity from a different package in my case? Tha activity is declared in the manifest file.


Thank you.


0 comments:

Post a Comment