将Intent与动作结合使用

时间:2011-04-19 11:32:32

标签: android android-activity android-intent action

右。我有一个需要调用另一个活动的动作。据我所知,如果我想解析这个活动的值,我需要使用Intents来实现。

然而,我的代码失败了,而且为什么会有点迷失。

我的主要活动:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.main);

    final ArrayList<menuItem> AMI = new ArrayList<menuItem>();
    /*Menu item: String name, String menu ID*/
    /*ToDo: Logic to fecth new menu structure from server*/
    menuItem MI1 = new menuItem("menu item 1","1");
    menuItem MI2 = new menuItem("menu item 2","2");
    AMI.add(MI1);
    AMI.add(MI2);

    GridView gridview = (GridView) findViewById(R.id.GridView01);
    gridview.setAdapter(new menuAdapter(this, AMI));
    gridview.setOnItemClickListener(new OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
        {
            //Toast.makeText(Runner.this, AMI.get(position).getMenuID(), Toast.LENGTH_SHORT).show();                
            Intent myIntent = new Intent(v.getContext(), showMenu.class);
            myIntent.putExtra("parentID", AMI.get(position).getMenuID());
            startActivityForResult(myIntent, 0);
        }
    });

“Toast”工作正常,但是当我调用showMenu类时它会崩溃。 showMenu类如下所示:

public class showMenu extends Activity{
public String menuParent = "";
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.submenu);

    Bundle extras = getIntent().getExtras();
    if(extras !=null)
    {
        menuParent = extras.getString("parentID");
    }
    /*ToDo: Logic to fecth new menu structure from server*/
    final ArrayList<menuItem> AMI = new ArrayList<menuItem>();
    menuItem MI1 = new menuItem("submenu 1","1");
    menuItem MI2 = new menuItem("submenu 2","2");
    AMI.add(MI1);
    AMI.add(MI2);

    GridView gridview = (GridView) findViewById(R.id.GridView01);
    gridview.setAdapter(new subMenuAdapter(this, AMI));
}

    public class subMenuAdapter extends BaseAdapter {
        private ArrayList<menuItem> MIL = new ArrayList<menuItem>();
        public static final int ACTIVITY_CREATE = 10;
        public subMenuAdapter(Context c, ArrayList<menuItem> AMI) {
            MIL = AMI;
        }
        public int getCount() {
            return MIL.size();

        }
        public Object getItem(int position) {
            return null;
        }
        public long getItemId(int position) {
            return 0;
        }
        public View getView(int position, View convertView, ViewGroup parent) {
            View v;
            if (convertView == null) {
                LayoutInflater li = getLayoutInflater();
                v = (LinearLayout) li.inflate(R.layout.grid_item, null);
                TextView tv = (TextView)v.findViewById(R.id.grid_text);
                tv.setText(MIL.get(position).getMenuname());

            } else {
                v = convertView;
            }
            return v;
        }
    }

}

知道它崩溃的原因吗?

4 个答案:

答案 0 :(得分:0)

我认为您必须在AndroidManifest.xml中注册您的Intent

<activity
        android:name="Package.Name.showMenu"
        android:theme="@android:style/Theme.Light"></activity>

答案 1 :(得分:0)

由于这两个活动都来自同一个应用程序,因此还有一些其他方法可以在两者之间传递数据而不是意图。检查以下链接:

http://developer.android.com/resources/faq/framework.html#3

如果您只想使用意图,请指出您所面临的错误,以便有人能够正确回复您。

答案 2 :(得分:0)

嘿.. plz显示menuAdapter类,bcose你必须从你的menuAdapter调用Intent到getView()类,而不是使用setOnItemClickListener Listner。

答案 3 :(得分:0)

如果要将值解析为另一个活动,则必须使用startActivityForResult。这称为sub_activity。