在listview中启动活动

时间:2014-04-12 06:55:32

标签: android android-intent android-listview

public class Menu extends ListActivity {

private String mstring[] = { "MainActivity", "Activity2", "Activity3",
        "Activity4" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(Menu.this,  android.R.layout.simple_list_item_1, mstring));
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    String positn = mstring[position];

    try {
        Class ourclass = Class.forName("com.example.list" + positn);
        Intent inten = new Intent(Menu.this, ourclass);
        startActivity(inten);
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    }

我想从listview开始活动,但这不起作用,如何在列表项单击上启动活动。

这里是logcat:

04-12 13:32:27.728: W/System.err(15518): java.lang.ClassNotFoundException: com.example.apaooo.MainActivity
04-12 13:32:27.728: W/System.err(15518):    at java.lang.Class.classForName(Native Method)
04-12 13:32:27.728: W/System.err(15518):    at java.lang.Class.forName(Class.java:204)
04-12 13:32:27.728: W/System.err(15518):    at java.lang.Class.forName(Class.java:169)
04-12 13:32:27.728: W/System.err(15518):    at com.example.appaooo.Menu.onListItemClick(Menu.java:29)
04-12 13:32:27.738: W/System.err(15518):    at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
04-12 13:32:27.738: W/System.err(15518):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
04-12 13:32:27.738: W/System.err(15518):    at android.widget.AbsListView.performItemClick(AbsListView.java:1104)
04-12 13:32:27.738: W/System.err(15518):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2792)
04-12 13:32:27.738: W/System.err(15518):    at android.widget.AbsListView$1.run(AbsListView.java:3468)
04-12 13:32:27.738: W/System.err(15518):    at android.os.Handler.handleCallback(Handler.java:730)
04-12 13:32:27.738: W/System.err(15518):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-12 13:32:27.738: W/System.err(15518):    at android.os.Looper.loop(Looper.java:213)
04-12 13:32:27.738: W/System.err(15518):    at android.app.ActivityThread.main(ActivityThread.java:5225)
04-12 13:32:27.738: W/System.err(15518):    at java.lang.reflect.Method.invokeNative(Native Method)
04-12 13:32:27.738: W/System.err(15518):    at java.lang.reflect.Method.invoke(Method.java:525)
04-12 13:32:27.738: W/System.err(15518):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
04-12 13:32:27.738: W/System.err(15518):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
04-12 13:32:27.738: W/System.err(15518):    at dalvik.system.NativeStart.main(Native Method)
04-12 13:32:27.738: W/System.err(15518): Caused by: java.lang.NoClassDefFoundError: com/example/apaooo/MainActivity
04-12 13:32:27.748: W/System.err(15518):    ... 18 more
04-12 13:32:27.748: W/System.err(15518): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.apaooo.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.appaooo-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.appaooo-2, /vendor/lib, /system/lib]]
04-12 13:32:27.748: W/System.err(15518):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
04-12 13:32:27.748: W/System.err(15518):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
04-12 13:32:27.748: W/System.err(15518):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
04-12 13:32:27.748: W/System.err(15518):    ... 18 more

MianActivity.java

package com.example.appaooo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

 public class MainActivity extends Activity {

Button btn;
    @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Context context = this;
    btn = (Button) findViewById(R.id.btnGeo);

    btn.setOnClickListener(new OnClickListener() {

        @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(context, WebViewActivity.class);
        startActivity(intent);
        }
    });
}

}

清单文件在这里

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appaooo"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:theme="@style/customTheme"
        android:name="com.example.appaooo.Menu"
        android:screenOrientation="portrait"
        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.appaooo.MainActivity"
           android:label="@string/app_name" ></activity>

</application>

首先我试图仅开始主要活动

2 个答案:

答案 0 :(得分:2)

更改此

Class ourclass = Class.forName("com.example.list" + positn);// .missing

Class ourclass = Class.forName("com.example.list."+positn); 

确保您已在清单文件中声明了所有活动

  

引起:java.lang.ClassNotFoundException:没有找到类   路径上的“com.example.apaooo.MainActivity”:DexPathList [[zip文件   “/data/app/com.example.appaooo-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.appaooo-2,/ vendor / lib,/ system / lib]]

您的包名可能是

com.example.apaooo

所以它应该是

Class ourclass = Class.forName("com.example.apaooo."+positn);  

答案 1 :(得分:0)

替换此行代码

Class ourclass = Class.forName("com.example.list" + positn);

这一行

Class ourclass = Class.forName("com.example.apaooo." + positn);

并确保使用此包名在Manifest.xml中注册每个活动。 enter image description here