2017-04-05 120 views
0

IM新手方法,我想與方法從MainActivity試圖插入到數據庫與MainActivity從片段與片段

這裏插入來自片段一些數據到數據庫是我的代碼 LaporanFragment

public class LaporanFragment extends Fragment{ 

EditText judulL, isiL; 
TextView nomor_ktp, ambilNama; 
ImageView fotoL; 
Button kirim; 
private ProgressDialog progressDialog; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 

    View v = inflater.inflate(R.layout.fragment_laporan, container, false); 

    judulL = (EditText) v.findViewById(R.id.judulLaporan); 
    isiL = (EditText) v.findViewById(R.id.isiLaporan); 

    nomor_ktp = (TextView) getActivity().findViewById(R.id.nomor_ktp); 
    final String noktp = nomor_ktp.getText().toString(); 
    //fotoL = (ImageView) v.findViewById(R.id.foto_laporan); 

    final String jdlLaporan = judulL.getText().toString(); 
    final String isiLaporan = isiL.getText().toString(); 

    kirim = (Button) v.findViewById(R.id.kirim_laporan); 

    kirim.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      ((MainActivity)getActivity()).kirim_lapor(jdlLaporan, isiLaporan, noktp); 
     } 
    }); 

    return v; 
} 

MainActivity與kirim_lapor方法

public void kirim_lapor(final String judul, final String isi, final String username){ 

    StringRequest stringRequest = new StringRequest(Request.Method.POST, 
      Constants.URL_LAPOR, 
      new Response.Listener<String>(){ 
       @Override 
       public void onResponse(String response){ 
        //progressDialog.dismiss(); 

        try{ 
         JSONObject jsonObject = new JSONObject(response); 

         //Toast.makeText(LaporanFragment.this, jsonObject.getString("message"), Toast.LENGTH_LONG).show(); 
        }catch(JSONException e){ 
         e.printStackTrace(); 
        } 
       } 
      }, 
      new Response.ErrorListener(){ 
       @Override 
       public void onErrorResponse(VolleyError error){ 
        //progressDialog.hide(); 
        //Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show(); 
       } 
      }){ 
     @Override 
     protected Map<String, String> getParams() throws AuthFailureError { 
      Map<String, String> params = new HashMap<>(); 
      params.put("judul_laporan", judul); 
      params.put("isi_laporan", isi); 
      params.put("no_ktp", username); 
      return params; 
     } 
    }; 

    RequestHandler.getInstance(this).addToRequestQueue(stringRequest); 
} 

問題是當我按按鈕kirim在片段中,應用程序將崩潰 請幫助我的傢伙,抱歉我的英語不好。

+0

把你的堆棧跟蹤與例外這裏 –

回答

0

爲了本地化和阻止進一步的錯誤,您應該製作一個監聽器來告訴Activity數據,而不是通過調用MainActivity方法來調用MainActivity方法。

在片段創建界面監聽器:

public class LaporanFragment extends Fragment { 

    private LaporanListener mListener; 

    // Define a Listener to 'speak up' to the main activity 
    public interface LaporanListener { 
     public void onSendReportClicked(String title, String content, String idNumber); 
    } 
    ... 
} 

當單擊該按鈕sendReport,使用監聽器:

public class LaporanFragment extends Fragment { 

    ... 
    ... 
    kirim.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      mListener.onSendReportClicked(jdlLaporan, isiLaporan, noktp); 
     } 
    }); 
    ... 
    ... 
} 

然後,你需要實現該接口監聽器在MainActivity:

public class MainActivity extends Activity implements LaporanFragment.LaporanListener { 

    ... 

    @Override 
    public void onSendReportClicked(String title, String content, String idNumber) { 
    // MainActivity will receive the data here. 
    // You need to process here. 
    } 

    ... 
} 

對於進一步的閱讀,閱讀Creating Custom Listeners

+0

當我試圖點擊按鈕的應用程序仍然崩潰,它說: 'E/AndroidRuntime(24882):致命異常:主要 E/AndroidRuntime(24882):過程:COM .example.banifahlevi.mediajenggala,PID:24882 E/AndroidRuntime(24882):java.lang.NullPointerException:試圖調用接口方法'void com.example.banifahlevi.mediajenggala.LaporanFragment $ LaporanListener.onSendReportClicked(java.lang.String ,java.lang.String,java.lang.String)'空對象引用 E/AndroidRuntime(24882):\t at com.example.banifahlevi.mediajenggala.LaporanFragment $ 1.onClick(LaporanFragment.java:70)' –

+0

什麼是錯誤'com.example.banifahle在70行的代碼vi.mediajenggala.LaporanFragment $ 1.onCl ICK(LaporanFragment.的java:70)'?你有沒有實現在活動界面?片段是否添加到活動中? –

+0

代碼是 'mListener.onSendReportClicked(jdlLaporan,isiLaporan,noktp);'。 在活動是我沒有實現的接口,並添加到活動 –