当我点击我的列表视图,我希望它带我到另一个活动,当我点击第二个位置,我希望它带我去一个不同的activity..etc第一的位置。我试图写code,当我点击我的名单上的第一项查看需要我的活动MrsClubb但每当我点击的项目它出现消息不幸应用程序名有停止工作,然后在应用程序关闭。
When I click on the first position of my list view I want it to take me to another activity, and when I click on the second position I want it to take me to a different activity..etc. I have tried to write code that when I click on the first item of my list view it takes me to the activity "MrsClubb" but whenever I click on the item it comes up with the message "unfortunately "app name" has stopped working" and then the app closes.
任何想法?
下面是code我的应用程序的各个位:
Here is the code for various bits of my app:
MainActivity.java
MainActivity.java
public class MainActivity extends ActionBarActivity {
DrawerLayout mDrawerLayout;
ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;
String[] mDrawerListItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer);
mDrawerList = (ListView)findViewById(android.R.id.list);
mDrawerListItems = getResources().getStringArray(R.array.drawer_list);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDrawerListItems));
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position) {
case 0:
Intent i = new Intent(MainActivity.this, MrsClubb.class);
startActivity(i);
}
mDrawerLayout.closeDrawer(mDrawerList);
}
});
mDrawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout,
toolbar,
R.string.drawer_open,
R.string.drawer_close){
public void onDrawerClosed(View v){
super.onDrawerClosed(v);
invalidateOptionsMenu();
syncState();
}
public void onDrawerOpened(View v){
super.onDrawerOpened(v);
invalidateOptionsMenu();
syncState();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle.syncState();
}
@Override
protected void onPostCreate(Bundle savedInstanceState){
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case android.R.id.home: {
if (mDrawerLayout.isDrawerOpen(mDrawerList)){
mDrawerLayout.closeDrawer(mDrawerList);
} else {
mDrawerLayout.openDrawer(mDrawerList);
}
return true;
}
default: return super.onOptionsItemSelected(item);
}
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jonatboard.jonat.htssoundboard" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
活动我想要的项目要带我去的时候是点击现在还是空的,只有有这个code。在它:
The activity I want the item to take me to when it is click is currently empty and only has this code in it:
public class MrsClubb {
}
如果您需要查看更多code来帮助你,那么请让我知道。
If you need to see any more code to help you then please let me know.
定义 MrsClubb
类
public class MrsClubb extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
setContentView(R.id.mrsClubbLayout);
....
....
}
}
和覆盖活动
类的的onCreate()办法(至少是)。另外补充申报的清单:
and override the onCreate()
method of the Activity
class (at the very least). Also add the declaration in the manifest:
<activity
android:name=".MrsClubb"
android:label="@string/mrs_clubb_activity_title" >
</activity>
上一篇:Facebook的Android SDK中ShareDialog不起作用不起作用、Andr、Facebook、oid
下一篇:在一个空对象引用android.content.Context.getContentResolver()对象、android、content、Context