片段中的Android自定义ArrayAdapter

时间:2011-12-15 22:28:44

标签: android listview row android-arrayadapter fragment

嗨,我正在制作一个简单的列表,我只想要一个图标标题和描述,并把它放在一个片段中,因为它是一个viewpager的一部分,但是我得到一个nullpointerexception,我无法弄清楚为什么。我的代码看起来像这样,我的所有其他页面都很好,只是这个列表我无法开始工作。

public class Test extends Fragment{

    View view;
    Context context;
    public static ListView lView;
    public static ArrayAdapter<String> leftList;

    public class MyCustomAdapter extends ArrayAdapter<String> {
        public MyCustomAdapter(Context context, int resource, int textViewResourceId, String[] dayOfWeek) {
            super(context, resource, textViewResourceId, dayOfWeek);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView label=(TextView)view.findViewById(R.id.rowTextView);
            label.setText(DayOfWeek[position]);
            ImageView icon=(ImageView)view.findViewById(R.id.listIcon);
            if (DayOfWeek[position]=="Sunday"){
                icon.setImageResource(R.drawable.file);
            }else{
                icon.setImageResource(R.drawable.icon);
            }
            TextView desc = (TextView)view.findViewById(R.id.description);
            if (DayOfWeek[position]=="Sunday"){
                desc.setText("Description1");
            }else{
                desc.setText("desc");
            }
            return view;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
        view = inflater.inflate(R.layout.leftlist, null);
        LinearLayout pasteLayout=(LinearLayout)view.findViewById(R.id.bottomBar);
        pasteLayout.setVisibility(LinearLayout.GONE);
        context = getActivity();
        lView = (ListView) view.findViewById(R.id.ListView01);
        Test est = new Test();
        Test.MyCustomAdapter teS = est.new MyCustomAdapter(context, R.layout.row, R.id.rowTextView, DayOfWeek);
        leftList = teS;
        lView.setAdapter(leftList);
        return view;
    }


    String[] DayOfWeek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
}

logcat中的错误

E/AndroidRuntime( 1304): java.lang.NullPointerException
E/AndroidRuntime( 1304):    at com.testing.Test$MyCustomAdapter.getView(Test.java:29)
E/AndroidRuntime( 1304):    at android.widget.AbsListView.obtainView(AbsListView.java:1445)
E/AndroidRuntime( 1304):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1220)
E/AndroidRuntime( 1304):    at android.widget.ListView.onMeasure(ListView.java:1131)
E/AndroidRuntime( 1304):    at android.view.View.measure(View.java:8363)
E/AndroidRuntime( 1304):    at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)
E/AndroidRuntime( 1304):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
E/AndroidRuntime( 1304):    at android.view.View.measure(View.java:8363)
E/AndroidRuntime( 1304):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3160)
E/AndroidRuntime( 1304):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
E/AndroidRuntime( 1304):    at android.view.View.measure(View.java:8363)
E/AndroidRuntime( 1304):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:595)
E/AndroidRuntime( 1304):    at android.view.View.measure(View.java:8363)
E/AndroidRuntime( 1304):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3160)
E/AndroidRuntime( 1304):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
E/AndroidRuntime( 1304):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
E/AndroidRuntime( 1304):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
E/AndroidRuntime( 1304):    at android.view.View.measure(View.java:8363)
E/AndroidRuntime( 1304):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3160)
E/AndroidRuntime( 1304):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
E/AndroidRuntime( 1304):    at android.view.View.measure(View.java:8363)
E/AndroidRuntime( 1304):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3160)
E/AndroidRuntime( 1304):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
E/AndroidRuntime( 1304):    at android.view.View.measure(View.java:8363)
E/AndroidRuntime( 1304):    at android.view.ViewRoot.performTraversals(ViewRoot.java:841)
E/AndroidRuntime( 1304):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1873)
E/AndroidRuntime( 1304):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 1304):    at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 1304):    at android.app.ActivityThread.main(ActivityThread.java:3703)
E/AndroidRuntime( 1304):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1304):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 1304):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
E/AndroidRuntime( 1304):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
E/AndroidRuntime( 1304):    at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager(  278):   Force finishing activity com.testing/.MainActivity
W/ActivityManager(  278): Activity pause timeout for HistoryRecord{408d4540 com.testing/.MainActivity}

任何帮助都将非常感谢,非常感谢我真的希望我能让这个工作起来我觉得我真的非常接近:)

2 个答案:

答案 0 :(得分:2)

您正在调用view.findViewById但视图尚未实例化。相反,使用在onCreateView中传递给您的布局inflater,可能将其传递给适配器的构造函数。

答案 1 :(得分:1)

您已将View声明为全局,但未实例化。您可以在代码中添加一行

view = convertView

作为适配器的getView方法的第一个语句......

或者您可以使用convertView.findViewById