使用ListActivity和ArrayAdapter找不到NullPointerException

时间:2012-12-09 16:09:08

标签: java android actionbarsherlock android-arrayadapter listactivity

你好stackoverflow, 现在我遇到了另一个问题,我正在使用自定义ArrayAdapter的ListActivity,所以我可以完全自定义我的Listview以包含TextViews,开关,复选框....我得到了一个N​​ullPointerException,我不知道它的起源在哪里!这是我目前的代码:

public class MenuActivity extends SherlockListActivity {
    ListView scroll;
    int time;


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


        try {
            scroll = this.getListView();



            this.setListAdapter(new MyAdapter(this.getApplicationContext()));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            finish();
        }




    }




    public View find(int x){

        return findViewById(x);

    }


    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub


        super.onListItemClick(l, v, position, id);



        switch (position){
        case (0):{
            Toast.makeText(getApplicationContext(), "Hello you just chose \"Splash\"", Toast.LENGTH_SHORT).show();
        }break;
        case (1):{
            Toast.makeText(getApplicationContext(), "Its another one here !", Toast.LENGTH_SHORT).show();
        }break;
        case (2):{
            Toast.makeText(getApplicationContext(), String.valueOf(id) +" and "+ position, Toast.LENGTH_SHORT).show();

        }break;
        }


    }

    public void showToast(String toast){
        Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show();
    }



}

class MyAdapter extends ArrayAdapter<String>{


    LayoutInflater inf = LayoutInflater.from(this.getContext());

    public MyAdapter(Context context) {
        super(context , 0 ,stringArray);
        inf.from(context);
        // TODO Auto-generated constructor stub
    }


    public static String[] stringArray= new String[]{"Splash"   , "Another Example" , "Future Checkable"};


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        if (convertView == null){
            inf.inflate(R.layout.normal_row, parent , false);
        }
        switch(position){
        case(0):
            convertView = (LinearLayout) inf.inflate(R.layout.row_switch, parent,false);

            break;
        }


        return convertView;
    }




}

我的StackTrace:

12-09 17:52:55.631: E/AndroidRuntime(11921): FATAL EXCEPTION: main
12-09 17:52:55.631: E/AndroidRuntime(11921): java.lang.NullPointerException
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.widget.AbsListView.obtainView(AbsListView.java:2161)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.widget.ListView.makeAndAddView(ListView.java:1831)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.widget.ListView.fillDown(ListView.java:674)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.widget.ListView.fillFromTop(ListView.java:735)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.widget.ListView.layoutChildren(ListView.java:1652)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.widget.AbsListView.onLayout(AbsListView.java:1994)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.view.View.layout(View.java:14003)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.view.ViewGroup.layout(ViewGroup.java:4375)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.view.View.layout(View.java:14003)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.view.ViewGroup.layout(ViewGroup.java:4375)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.view.View.layout(View.java:14003)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.view.ViewGroup.layout(ViewGroup.java:4375)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.view.View.layout(View.java:14003)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.view.ViewGroup.layout(ViewGroup.java:4375)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.view.Choreographer.doFrame(Choreographer.java:532)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.os.Handler.handleCallback(Handler.java:725)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.os.Looper.loop(Looper.java:137)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at android.app.ActivityThread.main(ActivityThread.java:5039)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at java.lang.reflect.Method.invokeNative(Native Method)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at java.lang.reflect.Method.invoke(Method.java:511)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-09 17:52:55.631: E/AndroidRuntime(11921):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:4)

改变这个:

if (convertView == null){
     inf.inflate(R.layout.normal_row, parent , false);
}

为:

if (convertView == null){
     convertView = inf.inflate(R.layout.normal_row, parent , false);
}

另外,关于ListView中有两种类型的行,您应该考虑实现适配器的getViewTypeCount()getItemViewType()方法(因为您当前的代码不能很好地工作)