简体   繁体   English

单击列表视图项时是否打开活动?

[英]Open activity when listview Item is clicked?

I'm trying to built this app which simple consist of a ListView and different other activities that will be called when an item from the view is clicked. 我正在尝试构建此应用程序,该应用程序简单地由ListView和单击视图中的项时将调用的其他其他活动组成。 I want to when i pressed string Rumus Ekonomi it will open RumusEkonomi.class but when I click string Rumus Ekonomi , it force closes. 我想当我按string Rumus Ekonomi ,它将打开RumusEkonomi.class但是当我单击string Rumus Ekonomi ,它会强制关闭。

here the code: 这里的代码:

public class Beranda extends ListActivity 公共类Beranda扩展ListActivity
{ String [] items = { "Rumus Ekonomi", "Rumus Manajemen", "Rumus Akutansi" }; {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);
    } 
}}

Any help would be appreciated :) 任何帮助,将不胜感激 :)

UPDATE 更新

Here is code manifest: 这是代码清单:

<?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>

First i declare <intent-filter> to android:name="com.example.rumus.Rumus" but when i click, it forces closes. 首先,我向android:name="com.example.rumus.Rumus"声明<intent-filter> ,但是当我单击时,它会强制关闭。 so i move the <intent-filter> to <activity android:name=".Beranda" but it forces closes too 所以我将<intent-filter>移到<activity android:name=".Beranda"但它也强制关闭

Use this where you are using intent: 在使用意图的地方使用此:

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

Instead of this: 代替这个:

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

You will need to provide an appropriate context to your Intents. 您将需要为Intent提供适当的上下文。 Change your onListItemClick() block to the following 将您的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);
} 
}

Edit: Change your Manifest file to this 编辑:将清单文件更改为此

<?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>

确保您在beranda_layout中的列表视图中使用的是android:id =“ @ android:id / list”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM