gpt4 book ai didi

带有 ListView 的 Android 选项卡 fragment

转载 作者:行者123 更新时间:2023-11-30 03:59:32 25 4
gpt4 key购买 nike

我在尝试运行我的应用程序时遇到错误。谁能帮我检查一下我的代码?我会列出我所有的代码。我正在尝试为我的应用程序实现 fragment 。

主 Activity .java

public class MainActivity extends SherlockFragmentActivity {

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

ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

actionBar.setDisplayShowTitleEnabled(true);

Tab tab = actionBar.newTab()
.setText("Apple")
.setTabListener((TabListener) new AppleFragment())
.setIcon(R.drawable.apple);

actionBar.addTab(tab);


}

}

苹果 fragment .java

public class AppleFragment extends ListActivity  {

static final String URL = "http://api.androidhive.info/pizza/?format=xml";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_COST = "cost";
static final String KEY_DESC = "description";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();


XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element

NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST));
map.put(KEY_DESC, parser.getValue(e, KEY_DESC));

// adding HashList to ArrayList
menuItems.add(map);
}

ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
R.id.name, R.id.desciption, R.id.cost });


setListAdapter(adapter);



}

public static class ArrayListFragment extends SherlockListFragment implements TabListener{


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){




return super.onCreateView(inflater, container, savedInstanceState);


}


@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.add(android.R.id.content, this);

ft.attach(this);
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.detach(this);
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

}
}

日志:

10-04 16:55:52.805: E/AndroidRuntime(26238): FATAL EXCEPTION: main
10-04 16:55:52.805: E/AndroidRuntime(26238): java.lang.RuntimeException: Unable to start activity ComponentInfo{in.wptrafficanalyzer.actionbarsherlocknavtab/in.wptrafficanalyzer.actionbarsherlocknavtab.MainActivity}: java.lang.ClassCastException: in.wptrafficanalyzer.actionbarsherlocknavtab.AppleFragment cannot be cast to com.actionbarsherlock.app.ActionBar$TabListener
10-04 16:55:52.805: E/AndroidRuntime(26238): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
10-04 16:55:52.805: E/AndroidRuntime(26238): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
10-04 16:55:52.805: E/AndroidRuntime(26238): at android.app.ActivityThread.access$600(ActivityThread.java:127)
10-04 16:55:52.805: E/AndroidRuntime(26238): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
10-04 16:55:52.805: E/AndroidRuntime(26238): at android.os.Handler.dispatchMessage(Handler.java:99)
10-04 16:55:52.805: E/AndroidRuntime(26238): at android.os.Looper.loop(Looper.java:137)
10-04 16:55:52.805: E/AndroidRuntime(26238): at android.app.ActivityThread.main(ActivityThread.java:4512)
10-04 16:55:52.805: E/AndroidRuntime(26238): at java.lang.reflect.Method.invokeNative(Native Method)
10-04 16:55:52.805: E/AndroidRuntime(26238): at java.lang.reflect.Method.invoke(Method.java:511)
10-04 16:55:52.805: E/AndroidRuntime(26238): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
10-04 16:55:52.805: E/AndroidRuntime(26238): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
10-04 16:55:52.805: E/AndroidRuntime(26238): at dalvik.system.NativeStart.main(Native Method)
10-04 16:55:52.805: E/AndroidRuntime(26238): Caused by: java.lang.ClassCastException: in.wptrafficanalyzer.actionbarsherlocknavtab.AppleFragment cannot be cast to com.actionbarsherlock.app.ActionBar$TabListener
10-04 16:55:52.805: E/AndroidRuntime(26238): at in.wptrafficanalyzer.actionbarsherlocknavtab.MainActivity.onCreate(MainActivity.java:30)
10-04 16:55:52.805: E/AndroidRuntime(26238): at android.app.Activity.performCreate(Activity.java:4465)
10-04 16:55:52.805: E/AndroidRuntime(26238): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
10-04 16:55:52.805: E/AndroidRuntime(26238): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
10-04 16:55:52.805: E/AndroidRuntime(26238): ... 11 more

最佳答案

错误在:

Tab tab = actionBar.newTab()
.setText("Apple")
.setTabListener((TabListener) new AppleFragment())
.setIcon(R.drawable.apple);

选项卡监听器不能是 new AppleFragment() 你应该传递一个有效的监听器,或者让你的 fragment 实现 TabListener

关于带有 ListView 的 Android 选项卡 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12723784/

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