每次我點擊按鈕吐司空,我試圖用字符串替換結果在新的實例,但它返回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類發送字符串到類購買並顯示該字符串時,按鈕單擊。我檢查結果,但它不是空的,當我祝酒。但是當我將它放在Buy類中時,它顯示爲空。
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);
}
}
}
你的問題是什麼? –
如何在打開碎片時添加參數。你還可以添加你的setArguments分數嗎?如果你將這些代碼放在你的onCreateView而不是onclick中,會發生什麼?你的論點是否也在那裏? –
getArguments()始終爲空值。你能否請檢查一下代碼是否有問題。 –