您好我正在为活动制作网格菜单,所有东西都运行良好,当我点击特定图像时,它显示其位置不像0 1。问题是,当我点击specfic图像时,它应该打开另一个意图,它不会给出任何错误,也不会打开其他意图。善意的指导
public class Mood extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mood);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
switch (position) {
case 0:
Intent intent = new Intent(v.getContext(),a.class);
startActivity(intent);
break;
case 1:
Intent intent4 = new Intent(v.getContext(),b.class);
startActivity(intent4);
break;
default:
break;
}
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View grid;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
grid = new View(mContext);
LayoutInflater inflater = getLayoutInflater();
grid = inflater.inflate(R.layout.moodcus, parent, false);
} else {
grid = (View) convertView;
}
ImageView imageView = (ImageView) grid.findViewById(R.id.imgview);
TextView textView = (TextView) grid.findViewById(R.id.txtview);
imageView.setImageResource(mThumbIds[position]);
textView.setText(mMoodnames[position]);
return grid;
}
// references to our images
private Integer[] mThumbIds = { R.drawable.icon,
R.drawable.icon };
private String[] mMoodnames = { "Angry", "Bored"};
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView android:id="@+id/imgview" android:layout_width="60dip"
android:layout_height="60dip" android:layout_gravity="center" />
<TextView android:id="@+id/txtview" android:layout_width="wrap_content"
android:textColor="#ffffff" android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
和
<?xml version="1.0" encoding="utf-8"?>
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"
android:verticalSpacing="5dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:columnWidth="80dp"
android:stretchMode="columnWidth"
android:gravity="center_horizontal"
android:scaleType="fitXY"
android:background="@drawable/ac"
/>
logcat的
07-13 05:16:17.642: ERROR/AndroidRuntime(28966): Uncaught handler: thread main exiting due to uncaught exception
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): android.content.ActivityNotFoundException: Unable to find explicit activity class {Dynamicgrid.test/Dynamicgrid.test.a}; have you declared this activity in your AndroidManifest.xml?
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at android.app.Activity.startActivityForResult(Activity.java:2789)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at android.app.Activity.startActivity(Activity.java:2895)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at Dynamicgrid.test.Mood$1.onItemClick(Mood.java:39)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at android.widget.AbsListView.onKeyUp(AbsListView.java:1757)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at android.widget.GridView.commonKey(GridView.java:1470)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at android.widget.GridView.onKeyUp(GridView.java:1382)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at android.view.KeyEvent.dispatch(KeyEvent.java:1249)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at android.view.View.dispatchKeyEvent(View.java:3683)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:758)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:760)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:760)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:760)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1691)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1111)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at android.app.Activity.dispatchKeyEvent(Activity.java:2045)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1667)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2390)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2360)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at android.os.Handler.dispatchMessage(Handler.java:99)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at android.os.Looper.loop(Looper.java:123)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at android.app.ActivityThread.main(ActivityThread.java:4595)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at java.lang.reflect.Method.invokeNative(Native Method)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at java.lang.reflect.Method.invoke(Method.java:521)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-13 05:16:17.782: ERROR/AndroidRuntime(28966): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
如果没有在AndroidManifest.xml中声明活动a
也b
在res / menu文件夹中输入此文件yourmenu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menuitem1"
android:title="Option1"
android:orderInCategory="1"
android:icon="@drawable/_icon1">
</item>
<item
android:id="@+id/menuitem2"
android:title="Option2"
android:orderInCategory="2"
android:icon="@drawable/_icon2">
</item>
<item
android:id="@+id/menuitem3"
android:title="Option3"
android:orderInCategory="3"
android:icon="@drawable/_icon3">
</item>
</menu>
从菜单中打开另一个活动...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// SELECTING MENU FROM RESOURCE
getMenuInflater().inflate(R.menu.ypurmenu, menu);
// RETURN TRUE TO DISPLAY MENU
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getTitle().equals("Option1") ){
startActivity(new Intent(getApplicationContext(), AnotherActivityX.class)); // It Can be your GridViewActivity
finish();
}
else if(item.getTitle().equals("Option2") ){
startActivity(new Intent(getApplicationContext(), AnotherActivityY.class));
finish();
}
else if(item.getTitle().equals("Option3") ){
startActivity(new Intent(getApplicationContext(), AnotherActivityZ.class));
finish();
}
return super.onOptionsItemSelected(item);
}
或者,如果您想在用户按下菜单按钮时直接打开任何活动,那么就会有简单的解决方案......
@Override
public boolean onCreateOptionsMenu(Menu menu) {
startActivity(new Intent(this, AnyActivity.class));//Also you can write finish() here if if you want to back at previous activity
return super.onCreateOptionsMenu(menu);
}