从Fragment中的editText获取值以使用Asynctask发送

时间:2016-10-31 02:37:44

标签: java android android-fragments android-asynctask

我有将数据发送到服务器的片段,但我的编辑文本始终为null,应用程序中没有错误只能从编辑文本中获取值

这是我的XML

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    </LinearLayout>

    <Button
        android:text="Submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/submit_laporan"
        android:layout_below="@+id/desc_masalah"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="36dp" />

    <TextView
        android:text="Judul"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView5"
        android:textSize="18sp"
        android:layout_alignParentTop="true"
        android:layout_marginTop="14dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:paddingLeft="10dp" />

    <TextView
        android:text="Deskripsi Masalah"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="19dp"
        android:id="@+id/textView4"
        android:textSize="18sp"
        android:layout_below="@+id/judul_masalah"
        android:layout_alignLeft="@+id/textView5"
        android:layout_alignStart="@+id/textView5"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:paddingLeft="10dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:ems="10"
        android:layout_marginTop="13dp"
        android:id="@+id/desc_masalah"
        android:layout_below="@+id/textView4"
        android:layout_alignLeft="@+id/textView4"
        android:layout_alignStart="@+id/textView4"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:paddingLeft="10dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/judul_masalah"
        android:layout_marginTop="13dp"
        android:layout_below="@+id/textView5"
        android:layout_alignLeft="@+id/textView5"
        android:layout_alignStart="@+id/textView5"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:paddingLeft="10dp" />

</RelativeLayout>

这是我的JAVA

    public class Report extends Fragment{
    EditText judul_masalah, desc_masalah;
    Button submit_laporan;
    //public Report(){}
    //RelativeLayout view;

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

        View V = inflater.inflate(R.layout.laporan, container, false);
        getActivity().setTitle("Pelaporan");

        /*submit_laporan.setOnClickListener(new View.OnClickListener(){
            String newJudul = judul_masalah.getText().toString();
            String newDeskripsi = desc_masalah.getText().toString();
            @Override
            public void onClick(View v){

                Toast.makeText(getActivity(), newJudul,
                        Toast.LENGTH_LONG).show();
                new sendData().execute(newJudul,newDeskripsi);
            }
        });*/
        return V;
    }

    public void onViewCreated(View V, Bundle savedInstanceState){
        judul_masalah = (EditText) V.findViewById(R.id.judul_masalah);
        desc_masalah = (EditText) V.findViewById(R.id.desc_masalah);
        submit_laporan = (Button) V.findViewById(R.id.submit_laporan);

        submit_laporan.setOnClickListener(new View.OnClickListener(){
            String newJudul = judul_masalah.getText().toString();
            String newDeskripsi = desc_masalah.getText().toString();
            @Override
            public void onClick(View v){

                Toast.makeText(getActivity(), newJudul,
                        Toast.LENGTH_LONG).show();
                //new sendData().execute(newJudul,newDeskripsi);
            }
        });
    }

    public class sendData extends AsyncTask<String, String, JSONObject>{
        HttpURLConnection conn;
        URL url = null;
        JSONParser jsonParser = new JSONParser();
        private static final String TAG_INFO = "info";
        private static final String LAPORAN_URL = "URL";

        @Override
        protected JSONObject doInBackground(String... args) {
            try {

                HashMap<String, String> params = new HashMap<>();
                params.put("judul", args[0]);
                params.put("deskripsi", args[1]);

                Log.d("request", "starting");

                JSONObject json = jsonParser.makeHttpRequest(
                        LAPORAN_URL, "POST", params);

                if (json != null) {
                    Log.d("JSON result", json.toString());

                    return json;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(JSONObject json) {
            String info = "";


            if (json != null) {
                //Toast.makeText(LoginActivity.this, json.toString(),
                //Toast.LENGTH_LONG).show();

                try {
                    info = json.getString(TAG_INFO);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
            if(info.equals("Success")) {
                Toast.makeText(getActivity(), "Data Berhasil Disimpan",
                        Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(getActivity(), "Data Gagal Disimpan",
                        Toast.LENGTH_LONG).show();
            }

        }
    }
}

任何人都可以帮我解决我的代码错误

1 个答案:

答案 0 :(得分:1)

你必须将你的getText()放在方法onClick()中。现在它在外面并且无法正常工作。

该行: &#34; String newJudul = judul_masalah.getText()。toString();&#34; 行后: &#34; public void onClick(查看v){&#34;