0

在我的应用程序中,我创建了Custom ListView.So 作为我的问题,我想在ListView.According的特定行中添加和删除一些视图。但它给了我空指针异常
连续有一个按钮(比如说添加),我正在将它看到到特定的位置。通过单击它,我正在添加一个带有 textView 和按钮的布局(比如说删除)。成功我可以添加新的布局。
但它不是删除添加的布局(空指针exp。)。
以下是适配器类:

public class TestAdapter extends ArrayAdapter<String> {
    Context con;
    ArrayList<String> listdata;

    public TestAdapter(Context context, int textViewResourceId,ArrayList<String> myitem) 
    {
        super(context, textViewResourceId, myitem);
        this.con = context;
        this.listdata = myitem;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
    {
        LayoutInflater inflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final LinearLayout.LayoutParams linearparams=new LinearLayout.LayoutParams(
                android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);

        if (convertView == null) 
        {
            convertView = inflater.inflate(R.layout.fieldlist, null);
        }
        TextView data = (TextView) convertView.findViewById(R.id.VTItemTextView);
        Button add=(Button)convertView.findViewById(R.id.add);

        final EditText amount=(EditText)convertView.findViewById(R.id.amount);
        final LinearLayout llmain=(LinearLayout)convertView.findViewById(R.id.llmain);

        if(listdata.get(position).equalsIgnoreCase("lorem"))
        {
            add.setVisibility(View.VISIBLE);

            add.setOnClickListener(new OnClickListener() 
            {                   
                @Override
                public void onClick(View v) 
                {                       
                    final LinearLayout llcontent=new LinearLayout(con);
                    llcontent.setLayoutParams(linearparams);
                    final TextView textView=new TextView(con);
                    textView.setLayoutParams(linearparams);
                    textView.setText(amount.getText().toString());
                    textView.setTag(position);
                    final Button delete=new Button(con);
                    delete.setLayoutParams(linearparams);
                    delete.setText("delete");                       
                    delete.setTag(position);
                    llcontent.addView(textView);
                    llcontent.addView(delete);
                    llmain.addView(llcontent);                      
                    notifyDataSetChanged();
                    delete.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                             Integer index = (Integer) v.getTag();
                        //here is Null Pointer Exception
                            llcontent.removeViewAt(index.intValue()); 

                                Integer index1 = (Integer)textView.getTag();
                                llcontent.removeViewAt(index1.intValue()); 

                            notifyDataSetChanged();

                        }
                    });
                }
            });
        }
        else
            {
                add.setVisibility(View.GONE);
                amount.setVisibility(View.GONE);
            }
        data.setText(listdata.get(position));
        return convertView;
    }
}


日志猫:

  FATAL EXCEPTION: main
java.lang.NullPointerException
at android.view.ViewGroup.removeViewInternal(ViewGroup.java:3603)
at android.view.ViewGroup.removeViewAt(ViewGroup.java:3567)
at com.example.testapp.TestAdapter$1$1.onClick(TestAdapter.java:91)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)


为了测试,我创建了一个单独的演示,它工作正常但是如果我在另一个应用程序中使用这个逻辑,它就不起作用。为什么会这样……?救救我……!

4

1 回答 1

0

尝试在您的代码中使用context对象而不是。MainActivity.this看来您使用了错误的上下文。请仔细检查您使用的行MainActivity.this

于 2013-05-16T07:13:17.403 回答