0

我创建了一个列表。现在,当我单击列表项时,我想运行一个 Java 类,它是另一个名称列表。

这是我的代码:

public class SecondAct extends ListActivity {
   private String[] items = { "Pending Retailers", "Ordered Retailers",
         "No Order Retailers", "Today's Plan", "Messages", "Daily Sales",
         "Pending Invoices", "Day Close", "Tools and Updates", "Exit" };

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

      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_multiple_choice, items);
      // Get the activity's ListView and set its choice mode as multiple choice
      getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
      setListAdapter(adapter);
   }

   protected void onListItemClick(ListView l, View v, int position, long id) {
      // TODO Auto-generated method stub

      super.onListItemClick(l, v, position, id);
      String selectionValue = "";
      selectionValue = (String) getListView().getItemAtPosition(position);

      Log.i("List Selection Value: ",
            (String) getListView().getItemAtPosition(position));

      if (selectionValue.equalsIgnoreCase("Pending Retailers")) {

         Intent intent = new Intent("my.com.npi.List");
         startActivity(intent);
         AlertDialog alertDialog = new AlertDialog.Builder(SecondAct.this)
               .create();
         alertDialog.setTitle("Pending Retailers Selected");

         alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

               // here we i add functions

            }
         });

         // alertDialog.setIcon(R.drawable.icon);
         alertDialog.show();
      }

      else if (selectionValue.equalsIgnoreCase("Exit")) {
         finish();
      }
   }
}

现在,当我单击第一项时,应用程序被强制关闭。我使用意图开始新活动,但它不起作用。无望的卡住了!请帮忙。

谢谢。

4

2 回答 2

1

首先将 Manifest 中的新活动注册为

在清单中的应用程序标记之前声明包,如

<package name="my.com.npi">

<activity android:name = ".List" />

然后在 Intent 中给出正确正确的类名称。不要像 my.com.npi 一样使用,只需导入那些包,将纯名称作为 List.class

Intent intent = new Intent(this,List.class);
         startActivity(intent);
于 2012-05-11T12:03:35.497 回答
1

如果您在自己的应用程序中有活动,则使用:

Intent electIntent = new Intent();
electIntent.setClass(SecondAct.this, List.class);
startActivity(electIntent);

并在 AndroidManifest

<activity
   android:name = ".List" />

使用此意图从您的应用程序中启动另一个应用程序活动:

Intent intent25 = new Intent(Intent.ACTION_MAIN).addCategory(
Intent.CATEGORY_LAUNCHER).setClassName("APP_PACKAGE_NAME",
"APP_PACKAGE_NAME.TestActivity").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_FROM_BACKGROUND).setComponent(new ComponentName("APP_PACKAGE_NAME",
"APP_PACKAGE_NAME.TestActivity"));
getApplicationContext().startActivity(intent25);
于 2012-05-11T10:35:54.103 回答