我有一个名为 main_activity.xml 的活动和两个名为 Main.java 和 Menu.java 的 java 类。在 Menu.java 类中,我扩展了 ListActivity 类。当我运行 myApp 时,它会弹出强制关闭。请帮忙。
下面是 Menu.java 的代码:
公共类菜单扩展 ListActivity{
String classes[]={"Main","Second","Third","China"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String selectedItem= classes[position];
try{
Class myClass = Class.forName("com.example.myapp."+selectedItem);
Intent myIntent=new Intent(Menu.this, myClass);
startActivity(myIntent);
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
Main.java 类代码:
公共类主要扩展活动{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent=new Intent("com.example.myapp.Menu");
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Mainfest 文件代码:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
<activity
android:name="com.example.myapp.Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myapp.Menu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
错误快速排序: