gpt4 book ai didi

java - onListviewclick 不起作用

转载 作者:行者123 更新时间:2023-12-01 13:59:34 24 4
gpt4 key购买 nike

您好,我为我的 Android 应用程序创建了一个列表菜单,并且创建了该菜单链接到的其他 Activity ,但是当我运行它并单击列表项时,它不会将我链接到其他 Activity

代码在这里

menu.java(已更新)

                            package com.example.taekwondobuddy.util;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class Menu extends ListActivity {

String classes[] = {"Tool","Techniques"} ;

@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, classes));
}


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cheese = classes[position];
try{
Class ourclass = Class.forName("com.example.taekwondobuddy" + cheese);
Intent ourIntent = new Intent(Menu.this, ourclass);
startActivity(ourIntent);
} catch(ClassNotFoundException e){
e.printStackTrace();

}
}

工具.java

                 package com.example.taekwondobuddy.util;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Tools extends ListActivity{

String classes[] = {"Counter","Accelermeter","Timer"} ;

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


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cheese = classes[position];
try{
Class ourclass = Class.forName("com.example.taekwondobuddy" + cheese);
Intent ourIntent = new Intent(Tools.this, ourclass);
startActivity(ourIntent);
} catch(ClassNotFoundException e){
e.printStackTrace();

}
}

}

Technqiues.java

                package com.example.taekwondobuddy.util;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Technqiues extends ListActivity {

String classes[] = {"Kicks","Sparring",} ;

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


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cheese = classes[position];
try{
Class ourclass = Class.forName("com.example.taekwondobuddy" + cheese);
Intent ourIntent = new Intent(Technqiues.this, ourclass);
startActivity(ourIntent);
} catch(ClassNotFoundException e){
e.printStackTrace();

}
}

}

list

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

<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:name="com.example.taekwondobuddy.util.Menu"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.taekwondobuddy.util.Tools"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.Tools" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.taekwondobuddy.util.Techniques"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.Techniques" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>


</manifest>

有什么想法吗?

最佳答案

使用

Class ourclass = Class.forName("com.example.taekwondobuddy.util." + cheese);

用于获取具有完整包名称的类名,因为当前您缺少 .util,并确保您已在 AndroidManifest.xml 中添加所有 Activity

在类数组中“Tkd Buddy”不是有效的类名,因此请使用正确的类命名约定

关于java - onListviewclick 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19417107/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com