当我按下按钮时,它始终是空的

时间:2017-07-30 13:31:17

标签: android

每次我点击按钮它toast null,我尝试用字符串替换新实例中的结果但​​它返回null。

public class Buy extends Fragment implements View.OnClickListener{


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


        View rootView = inflater.inflate(R.layout.buy, container, false);
        Button b = (Button) rootView.findViewById(R.id.press);
        b.setOnClickListener(this);
        return rootView;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.press:
                if (getArguments() != null) {
                    String mParam1 = getArguments().getString("ARG_PARAM1");
                    Toast.makeText(getActivity(), mParam1, Toast.LENGTH_LONG).show();
            }
            else{
                Toast.makeText(getActivity(), "null", Toast.LENGTH_LONG).show();
            }
        }
    }

    public static Buy newInstance(String result) {
        Buy fragment = new Buy();
        Bundle args = new Bundle();
        args.putString("ARG_PARAM1",result);
        fragment.setArguments(args);
        return fragment;
    }
}

我想将BackgroundTask1类中的String发送到类Buy并在按钮单击时显示该String。我检查结果,但当我举杯时它不为空。但是当我在Buy类中显示它时,它显示为null。

  public class BackgroundTask1 extends AsyncTask<String,Void,String> {
        Context ctx;
        String data;

        // AlertDialog alertDialog;
        public BackgroundTask1(Context ctx) {
            this.ctx = ctx;
        }
       @Override
        protected void onPreExecute() {
            super.onPreExecute();
           /* alertDialog = new AlertDialog.Builder(ctx).create();
            alertDialog.setTitle("Login Information..");*/
        }

        @Override
        protected String doInBackground(String... params) {
            String store_url = "http://192.168.0.108//webapp/store.php";//
            String type = params[0];
            if (type.equals("store")) {
                String Bookname1 = params[1];
                String Bookcondition1 = params[2];
                String Postdate1 = params[3];
                String Expirydate1 = params[4];
                String Description1 = params[5];
                String Sellername1 = params[6];
                String Sellerlocation1 = params[7];
                String Sellercontact1 = params[8];

                try {
                    URL url = new URL(store_url);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("POST");
                    httpURLConnection.setDoOutput(true);
                    httpURLConnection.setDoInput(true);
                    OutputStream OS = httpURLConnection.getOutputStream();
                    BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
                    String post_data = URLEncoder.encode("Bookname1", "UTF-8") + "=" + URLEncoder.encode(Bookname1, "UTF-8") + "&"
                            + URLEncoder.encode("Bookcondition1", "UTF-8") + "=" + URLEncoder.encode(Bookcondition1, "UTF-8") + "&"
                            + URLEncoder.encode("Postdate1", "UTF-8") + "=" + URLEncoder.encode(Postdate1, "UTF-8") + "&"
                            + URLEncoder.encode("Expirydate1", "UTF-8") + "=" + URLEncoder.encode(Expirydate1, "UTF-8") + "&"
                            + URLEncoder.encode("Description1", "UTF-8") + "=" + URLEncoder.encode(Description1, "UTF-8") + "&"
                            + URLEncoder.encode("Sellername1", "UTF-8") + "=" + URLEncoder.encode(Sellername1, "UTF-8") + "&"
                            + URLEncoder.encode("Sellerlocation1", "UTF-8") + "=" + URLEncoder.encode(Sellerlocation1, "UTF-8") + "&"
                            + URLEncoder.encode("Sellercontact1", "UTF-8") + "=" + URLEncoder.encode(Sellercontact1, "UTF-8") + "&";
                    bufferedWriter.write(post_data);
                    bufferedWriter.flush();
                    bufferedWriter.close();
                    OS.close();
                    InputStream IS = httpURLConnection.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(IS, "iso-8859-1"));
                    StringBuilder sb = new StringBuilder();
                    String result = "";
                    String line = null;
                    while ((line = bufferedReader.readLine()) != null) {
                        result += line;
                    }
                    httpURLConnection.disconnect();
                    IS.close();
                    return result; //"Posted Successfully..."

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    // e.printStackTrace();
                    return "exception";
                }
            }

            return null;
        }


        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(String result) {
            if (result.equals("Posted Successfully...")) {
                Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();

                // Intent i=new Intent(this,Sell.class);
            } else if (result.equals("nullpointer")) {
                Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();

            } else {
                Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
            Buy fragment = Buy.newInstance(result);
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您不会调用片段管理器将片段放在屏幕上,只需创建其实例即可。 尝试在onPostExecute方法中添加回调。

在你的片段中创建:

public interface FragmentCallback {
    public void onTaskDone(String result);
}

在您的活动中,在AsyncTask构造函数签名中添加此侦听器:

new BackgroundTask1(this, new Buy.FragmentCallback() {
    @Override
    public void onTaskDone(String result) {
        Buy fragment = Buy.newInstance(result);
        getSupportFragmentManager().beginTransaction()
           .add(R.id.place_for_ur_fragment, fragment).commit();
    }
}).execute();

AsynkTask中创建回调变量,更改构造函数和onPostExecute方法:

private Buy.FragmentCallback mFragmentCallback;

public BackgroundTask1(Context ctx, Buy.FragmentCallback fragmentCallback) {
    mFragmentCallback = fragmentCallback;
    this.ctx = ctx;
}
@Override
protected void onPostExecute(String result) {
    mFragmentCallback.onTaskDone(result);
}

希望这有帮助。