2017-07-30 4 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; 
    } 
} 

ボタンをクリックすると、StringをBackgroundTask1クラスからクラスに購入して表示したいと思います。私は結果をチェックしますが、私はそれをトーストするとnullではありません。私が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

あなたの質問は何ですか? –

+1

Fragmentを開いている間、どのように引数を追加していますか? setArgumentsのスコアを追加することはできますか?また、onclickではなくonCreateViewにこのコードを配置するとどうなりますか?あなたの引数もそこにヌルですか? –

+0

getArguments()は常にNULL値を取得しています。コードにいくつかの欠陥があるかどうか確認してください。 –

答えて

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メソッドをコールバックVARを作成します。

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

希望この助け。

+0

エラー:シンボルメソッドを見つけることができませんgetSupportFragmentManager() –

+0

あなたのアクティビティはAppCompatActivityを拡張しますか? – Mike

関連する問題