如何使用按钮单击侦听器来扩充布局?

时间:2016-07-19 06:14:39

标签: android web-services

我从.net网络服务获取数据并尝试在列表视图中显示它。我编写的编码如下:

public class DisplayList extends AppCompatActivity {

private static final String TAG_CATEGORY = "main_name";
private static final String TAG_SUBCAT = "cat_name";
private static final String TAG_CODE = "code";
private static final String TAG_NAME = "name";
private static final String TAG_RATE = "rate";
private static final String TAG_RATE2 = "rate2";
private static final String TAG_RATE3 = "rate3";
private static final String TAG_IMAGE = "img";

ImageAdapter mImageAdapter = null;
ListView mListView = null;

ArrayList<DetailsVO> mListDetails = null;

String subc;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_list);

    mListView = (ListView) findViewById(R.id.list_view);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        subc = extras.getString("Subcat");
    }

    new AsyncCallSoap1().execute();

}

public class AsyncCallSoap1 extends AsyncTask<String,Void,String> {
    private final ProgressDialog dialog=new ProgressDialog(DisplayList.this);

    @Override
    protected String doInBackground(String... params) {
        CallSoap com=new CallSoap();
        String SOAP_ACTION="http://tempuri.org/IprotechService/SelectSuCatgeory";
        String OPERATION_NAME="SelectSuCatgeory";
        String response = com.getDetails(OPERATION_NAME,SOAP_ACTION);

        if(response!=null){
            try {

                JSONArray array1 = new JSONArray(response);

                for(int i=0;i<array1.length();i++)
                {

                    final DetailsVO detailsVO = new DetailsVO();
                    JSONObject obj1 = array1.getJSONObject(i);

                    String out_category = obj1.getString(TAG_CATEGORY);
                    String out_subcat = obj1.getString(TAG_SUBCAT);
                    String out_code = obj1.getString(TAG_CODE);
                    String out_name = obj1.getString(TAG_NAME);
                    String out_rate = obj1.getString(TAG_RATE);
                    String out_rate2 = obj1.getString(TAG_RATE2);
                    String out_rate3 = obj1.getString(TAG_RATE3);
                    String out_img = obj1.getString(TAG_IMAGE);

                    // Toast.makeText(DetailsList.this, categoryId, Toast.LENGTH_LONG).show();

                    detailsVO.setName(out_name);
                    detailsVO.setRate(out_rate);

                    mListDetails.add(detailsVO);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return response;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //Showing progress dialog
        dialog.setMessage("Please wait...");
        dialog.setCancelable(false);
        dialog.show();
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        dialog.dismiss();

        if(mListDetails != null){
            mImageAdapter = new ImageAdapter(DisplayList.this, mListDetails);
            mListView.setAdapter(mImageAdapter);
        }

    }
}

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<DetailsVO> mItems;
    private ArrayList<DetailsVO> mOriginalItems;

    public ImageAdapter(Context context, ArrayList<DetailsVO> items) {
        this.mContext = context;
        this.mItems = items;
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public DetailsVO getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public ArrayList<DetailsVO> getItems(){
        return mItems;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.row_layout, null);

        }

        DetailsVO detailsVO = mItems.get(position);

        View listView = (View) convertView;

        // set value into textview
        TextView textTitle = (TextView) listView.findViewById(R.id.textView2);

        TextView textRate = (TextView) listView.findViewById(R.id.textView3);

        Button btnMap = (Button) listView.findViewById(R.id.btn_add);
        btnMap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(v.getContext(), "Feature coming soon...", Toast.LENGTH_SHORT).show();

            }
        });

        textTitle.setText(detailsVO.getName());
        textRate.setText(detailsVO.getRate());

        btnMap.setTag(detailsVO);

        return listView;
    }

}

但我无法夸大布局。我得到的错误如下所述。请帮我解决问题

07-19 11:40:44.503 28989-29218/com.example.mayur.easygrocery E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                                           java.lang.RuntimeException: An error occured while executing doInBackground()
                                                                               at android.os.AsyncTask$3.done(AsyncTask.java:299)
                                                                               at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
                                                                               at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
                                                                               at java.util.concurrent.FutureTask.run(FutureTask.java:239)
                                                                               at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
                                                                               at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
                                                                               at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
                                                                               at java.lang.Thread.run(Thread.java:856)
                                                                            Caused by: java.lang.NullPointerException
                                                                               at com.example.mayur.easygrocery.DisplayList$AsyncCallSoap1.doInBackground(DisplayList.java:108)
                                                                               at com.example.mayur.easygrocery.DisplayList$AsyncCallSoap1.doInBackground(DisplayList.java:73)
                                                                               at android.os.AsyncTask$2.call(AsyncTask.java:287)
                                                                               at java.util.concurrent.FutureTask.run(FutureTask.java:234)
                                                                               at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
                                                                               at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
                                                                               at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
                                                                               at java.lang.Thread.run(Thread.java:856) 
07-19 11:40:45.104 28989-28989/com.example.mayur.easygrocery E/WindowManager: Activity com.example.mayur.easygrocery.DisplayList has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42355370 V.E..... R......D 0,0-456,144} that was originally added here
                                                                          android.view.WindowLeaked: Activity com.example.mayur.easygrocery.DisplayList has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42355370 V.E..... R......D 0,0-456,144} that was originally added here
                                                                              at android.view.ViewRootImpl.<init>(ViewRootImpl.java:464)
                                                                              at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:235)
                                                                              at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:73)
                                                                              at android.app.Dialog.show(Dialog.java:282)
                                                                              at com.example.mayur.easygrocery.DisplayList$AsyncCallSoap1.onPreExecute(DisplayList.java:124)
                                                                              at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
                                                                              at android.os.AsyncTask.execute(AsyncTask.java:534)
                                                                              at com.example.mayur.easygrocery.DisplayList.onCreate(DisplayList.java:69)
                                                                              at android.app.Activity.performCreate(Activity.java:5326)
                                                                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
                                                                              at android.app.ActivityThread.access$700(ActivityThread.java:157)
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                              at android.os.Looper.loop(Looper.java:176)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5317)
                                                                              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:1102)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
                                                                              at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:-1)

因为mListDetails没有初始化。您可以在onPreExecuteOnCreate()

中初始化
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                //Showing progress dialog
                mListDetails = new Arraylist<DetailsVO>();
                dialog.setMessage("Please wait...");
                dialog.setCancelable(false);
                dialog.show();
            }