您好,我是Android编程的新手,我遇到两个错误,无法弄清楚我曾尝试使用Google搜索问题,但仍然没有运气,所以我的最后选择是应该这样做!我正在创建具有多个带有滑动手势的选项卡的Android启动器,从一个片段到另一个片段,我的所有屏幕都可以正常工作,除了书架视图活动(即使它说活动是片段)
这是我不了解如何解决的两个错误:
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上崩溃
继承人的日志:
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
任何链接或参考资料可以告诉我我做错了什么,这太棒了
预先感谢1,000,000次
最佳答案
ListFragment
扩展Fragment
而不是Activity
。您不想在片段中调用setContentView
。您应该从onCreateView
返回视图。
检查以下问题:
How to setContentView in a fragment?
Android - ListFragment fails on setContentView
关于android - 对于类型ShelfViewActivity,未定义方法setContentView(int),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19916412/