gpt4 book ai didi

android - ShelfViewActivity 类型未定义方法 setContentView(int)

转载 作者:行者123 更新时间:2023-11-29 21:28:51 25 4
gpt4 key购买 nike

你好,我是 Android 编程的新手,我有两个错误,我无法弄清楚我尝试用谷歌搜索这个问题,但仍然没有运气,所以我最后的选择是应该的!我正在创建 Android 启动器,它有多个选项卡,可以通过滑动手势从一个 fragment 转到另一个 fragment

这是我遇到的两个错误,不知道如何修复:

Multiple markers at this line
- The method setContentView(int) is undefined for the type
ShelfViewActivity
- The method setContentView(int) is undefined for the type
ShelfViewActivity


error2:

The constructor ShelfViewActivity.VerticalAdapter(ShelfViewActivity, int, ArrayList<ArrayList<BookItem>>) is
undefined

这是 ShelfViewActivity:

package com.d4a.tobias;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ShelfViewActivity extends ListFragment {
/** Called when the activity is first created. */
private VerticalAdapter verListAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.main, container, false);
}

/*
* Calling Library & BookItem classes for create list of groups
* groupbyArrayBookItem return back array of array of items
*/
Library lb = new Library();{
for (BookItem item : BookItem.ALL_BOOKS) {
lb.addBookItem(item);
} //line 32
ArrayList<ArrayList<BookItem>> groupList = new ArrayList<ArrayList<BookItem>>();
groupList = lb.groupbyArrayBookItem(Library.AUTHOR);

verListAdapter = new VerticalAdapter(getActivity(), R.layout.bookrow, groupList);
setListAdapter(verListAdapter);

verListAdapter.notifyDataSetChanged();
}

/**
* This class add a list of ArrayList to ListView that it include multi
* items as bookItem.
*/
private class VerticalAdapter extends ArrayAdapter<ArrayList<BookItem>> {

private int resource;

public VerticalAdapter(Context _context, int _ResourceId,
ArrayList<ArrayList<BookItem>> _items) {
super(_context, _ResourceId, _items); //line 52
this.resource = _ResourceId;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;

if (convertView == null) {
rowView = LayoutInflater.from(getContext()).inflate(resource,
null);
} else {
rowView = convertView;
}

HorizontalListView hListView = (HorizontalListView) rowView
.findViewById(R.id.subListview);
HorizontalAdapter horListAdapter = new HorizontalAdapter(
getContext(), R.layout.item, getItem(position));
hListView.setAdapter(horListAdapter);

return rowView;
}
}

/*
* This class add some items to Horizontal ListView this ListView include
* several bookItem.
*/
private class HorizontalAdapter extends ArrayAdapter<BookItem> {

private int resource;

public HorizontalAdapter(Context _context, int _textViewResourceId,
ArrayList<BookItem> _items) {
super(_context, _textViewResourceId, _items);
this.resource = _textViewResourceId;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View retval = LayoutInflater.from(getContext()).inflate(
this.resource, null);

TextView topText = (TextView) retval.findViewById(R.id.title);
TextView bottomText = (TextView) retval
.findViewById(R.id.author);

topText.setText(getItem(position).getAuthor());
bottomText.setText(getItem(position).getTitle());

return retval;
}
}
}

编辑:应用程序现在启动但我在 Shelf View Activity 上崩溃

这是 logcat:

    11-11 17:17:44.500: E/AndroidRuntime(23305): FATAL EXCEPTION: main
11-11 17:17:44.500: E/AndroidRuntime(23305): Process: com.d4a.tobias, PID: 23305
11-11 17:17:44.500: E/AndroidRuntime(23305): java.lang.NullPointerException
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
11-11 17:17:44.500: E/AndroidRuntime(23305): at com.d4a.tobias.ShelfViewActivity$VerticalAdapter.<init>(ShelfViewActivity.java:52)
11-11 17:17:44.500: E/AndroidRuntime(23305): at com.d4a.tobias.ShelfViewActivity.<init>(ShelfViewActivity.java:36)
11-11 17:17:44.500: E/AndroidRuntime(23305): at it.gmariotti.android.examples.googleaccount.MainFrag$MyAdapter.getItem(MainFrag.java:80)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:95)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.support.v4.view.ViewPager.populate(ViewPager.java:783)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1280)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.support.v4.view.ViewPager.computeScroll(ViewPager.java:1176)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.View.getDisplayList(View.java:13315)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.View.getDisplayList(View.java:13372)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.View.getDisplayList(View.java:13268)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.View.getDisplayList(View.java:13372)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.View.getDisplayList(View.java:13268)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.View.getDisplayList(View.java:13372)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.View.getDisplayList(View.java:13268)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.View.getDisplayList(View.java:13372)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.View.getDisplayList(View.java:13268)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.View.getDisplayList(View.java:13372)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1570)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1449)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.ViewRootImpl.draw(ViewRootImpl.java:2379)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2251)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1881)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.Choreographer.doFrame(Choreographer.java:532)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.os.Handler.handleCallback(Handler.java:733)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.os.Handler.dispatchMessage(Handler.java:95)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.os.Looper.loop(Looper.java:137)
11-11 17:17:44.500: E/AndroidRuntime(23305): at android.app.ActivityThread.main(ActivityThread.java:4998)
11-11 17:17:44.500: E/AndroidRuntime(23305): at java.lang.reflect.Method.invokeNative(Native Method)
11-11 17:17:44.500: E/AndroidRuntime(23305): at java.lang.reflect.Method.invoke(Method.java:515)
11-11 17:17:44.500: E/AndroidRuntime(23305): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-11 17:17:44.500: E/AndroidRuntime(23305): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-11 17:17:44.500: E/AndroidRuntime(23305): at dalvik.system.NativeStart.main(Native Method)
11-11 17:17:51.318: E/ACDB-LOADER(177): Error: ACDB AudProc vol returned = -8

任何向我展示我做错了什么的链接或引用都是很棒的

提前一百万次致谢

最佳答案

ListFragment 扩展 Fragment 而不是 Activity。您不想在 fragment 中调用 setContentView。您应该从 onCreateView 返回一个 View 。

检查以下问题:

How to setContentView in a fragment?

Android - ListFragment fails on setContentView

关于android - ShelfViewActivity 类型未定义方法 setContentView(int),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19916412/

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