2017-07-30 86 views
-1

每次我点击按钮吐司空,我试图用字符串替换结果在新的实例,但它返回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); 
      } 
     } 
    } 
+1

你的问题是什么? –

+1

如何在打开碎片时添加参数。你还可以添加你的setArguments分数吗?如果你将这些代码放在你的onCreateView而不是onclick中,会发生什么?你的论点是否也在那里? –

+0

getArguments()始终为空值。你能否请检查一下代码是否有问题。 –

回答

0

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

在您的片段创建此:

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方法创建回调VAR:

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

在你的活动中AsyncTask构造签名添加此监听器

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

Hope这个帮助。

+0

错误:无法找到符号方法getSupportFragmentManager() –

+0

您的活动是否扩展AppCompatActivity? – Mike