0
我有一个活动,可以将书签添加到收藏夹列表中。每个最爱都有一个按钮。当我点击那个按钮时,我必须去那个特定的活动。但它显示了我的错误。该应用程序不会崩溃,但书签活动不会从点击开始。无法启动另一个包中的活动(来自意图)
这里是代码为 “收藏” 的活动:
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");
}
}
的错误是OnClick方法,正是在这一行:
cl = Class.forName(myClass);
它给了我一个ClassNotFoundException。
看来,使用这段代码,我可以启动一个在这个包里面的活动。但问题是,我想开始的活动是在一个不同的包中。 logcat说它不能在当前包中找到活动。
那么如何在我的情况下从另一个包中打开活动? Tha活动在清单文件中声明。
谢谢。
谢谢,但没有工作。它说,进口不使用 – 2014-11-02 08:57:43
这里是答案,我认为... http://stackoverflow.com/questions/13326890/class-forname-and-classnotfoundexception?answertab=active#tab-top – 2014-11-02 19:29:38