对于ShelfViewActivity类型,方法setContentView(int)未定义

时间:2013-11-11 21:35:01

标签: android android-fragments

你好,我是Android编程的新手,我有两个错误,我无法弄清楚我试过谷歌搜索问题,但仍然没有运气,所以我的最后的手段就像它应该是!我正在创建Android启动器,它有多个带滑动手势的标签,可以从一个片段到另一个片段,除了shelfviewactivity(即使它说活动是一个片段),我的所有屏幕都工作得很好。

这是我不明白如何修复的两个错误:

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

任何链接或参考资料向我展示我做错了什么都很棒

提前感谢1,000,000次

1 个答案:

答案 0 :(得分:2)

ListFragment延伸Fragment而非Activity。您不希望在片段中调用setContentView。您应该从onCreateView返回一个视图。

检查以下问题:

How to setContentView in a fragment?

Android - ListFragment fails on setContentView