1

こんにちは、stackoverflow です。別の問題に遭遇しました。カスタム ArrayAdapter から ListActivity に取り組んでいるので、Listview を完全にカスタマイズして TextViews 、スイッチ、チェックボックスを含めることができます。 ! ここに私の現在のコードがあります:

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;
    }




}

そして私のスタックトレース:

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)
4

1 に答える 1

4

これを変える:

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

に:

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

また、2種類の行を含めることに関しては、アダプターのメソッドとメソッドのListView実装を検討する必要があります(現在のコードはうまく機能しないため)。getViewTypeCount()getItemViewType()

于 2012-12-09T16:13:54.200 に答える