单击listview项时打开活动?

时间:2015-06-07 05:51:26

标签: android listview android-intent android-activity

我正在尝试构建此应用程序,该应用程序简单地包含ListView以及在单击视图中的项目时将调用的其他活动。我希望当我按下string Rumus Ekonomi它会打开RumusEkonomi.class但是当我点击string Rumus Ekonomi时,它会强行关闭。

这里是代码:

公共类Beranda扩展了ListActivity
{     String [] items = {" Rumus Ekonomi"," Rumus Manajemen"," Rumus Akutansi" };

public void onCreate(Bundle icicle) 
{
    super.onCreate(icicle);
    setContentView(R.layout.beranda_layout);
    setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
}

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

    if (position == 0) 
    {
        Intent intent1 = new Intent(this, RumusEkonomi.class);
        startActivity(intent1);
    }
    else if (position == 1) 
    {
        Intent intent2 = new Intent(this, RumusManajemen.class);
        startActivity(intent2);
    }
    else if (position == 2) 
    {
        Intent intent3 = new Intent(this, RumusAkutansi.class);
        startActivity(intent3);
    } 
}}

任何帮助将不胜感激:)

更新

这是代码清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.rumus"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.rumus.Rumus"
        android:label="@string/app_name">
   </activity>
   <activity android:name=".Beranda" 
        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=".RumusEkonomi"
        android:label="@string/app_name" >    
    </activity> 
</application>
</manifest>

首先我向<intent-filter>声明android:name="com.example.rumus.Rumus"但是当我点击时,它会强制关闭。所以我将<intent-filter>移动到<activity android:name=".Beranda",但强制关闭

3 个答案:

答案 0 :(得分:0)

在使用意图的地方使用它:

Intent intent1 = new Intent(getApplicationContext(), YourClassName.class);  //Change here use getApplicationContext()
startActivity(intent1);

而不是:

Intent intent1 = new Intent(this, YourClassName.class);
startActivity(intent1);

答案 1 :(得分:0)

您需要为Intent提供适当的上下文。将您的onListItemClick()块更改为以下

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

if (position == 0) {
    Intent intent1 = new Intent(Beranda.this, RumusEkonomi.class);
    startActivity(intent1);
}
else if (position == 1) {
    Intent intent2 = new Intent(Beranda.this, RumusManajemen.class);
    startActivity(intent2);
}
else if (position == 2) {
    Intent intent3 = new Intent(Beranda.this, RumusAkutansi.class);
    startActivity(intent3);
} 
}

编辑:将您的清单文件更改为此

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
   <activity android:name=".Beranda" 
             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=".RumusEkonomi"
        android:label="@string/app_name" >    
   </activity> 
   <activity
        android:name=".RumusManajemen"
        android:label="@string/app_name" >
   </activity>
   <activity
        android:name=".RumusAkutansi"
        android:label="@string/app_name" >
   </activity>
</application>
</manifest>

答案 2 :(得分:0)

确保您使用的是android:id =&#34; @android:id / list&#34;对于beranda_layout中的列表视图。