单击ActionBar中的项目会导致Android应用程序崩溃

时间:2014-08-14 09:57:47

标签: android android-intent android-activity android-actionbar

我正处于Android开发过程的初始阶段,现在我正在尝试让应用响应其操作栏中项目的点击。在我的主要行动中,我有这个代码:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    switch(item.getItemId()) {
    case R.id.action_one:
        Intent intent = new Intent(this, ListaActivity.class);
        startActivity(intent);
        return true;
    case R.id.action_two:
        Toast.makeText(getApplicationContext(), "two", Toast.LENGTH_LONG).show();
        return true;
    case R.id.action_three:
        Toast.makeText(getApplicationContext(), "three", Toast.LENGTH_LONG).show();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

当我在模拟器或设备中运行应用程序,并尝试访问相应的项目时,应用程序崩溃并退出。任何人都可以告诉我这里有什么问题?

ListaActivity的代码

public class ListaActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String[] myStringArray = {"Windows", "Linux", "MacOSX"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myStringArray );
        ListView listView = (ListView) findViewById(R.id.listView1);
        listView.setAdapter(adapter);

        // Create a message handling object as an anonymous class.
        OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub
            }
        };
        listView.setOnItemClickListener(mMessageClickedHandler);

        setContentView(R.layout.activity_lista);
    }

}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hello"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/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>
        <activity
            android:name=".DisplayMessageActivity"
            android:label="@string/title_activity_display_message"
            android:parentActivityName=".MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.hello.MainActivity" />
        </activity>
        <activity
            android:name=".ListaActivity"
            android:label="@string/title_activity_lista"
            android:parentActivityName=".MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.hello.MainActivity" />
        </activity>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:3)

试试这个..

在初始化setContentView(R.layout.activity_lista);之前添加ListView,如

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_lista);
    String[] myStringArray = {"Windows", "Linux", "MacOSX"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myStringArray );
    ListView listView = (ListView) findViewById(R.id.listView1);