ErenRavenHeart
ErenRavenHeart

Reputation: 281

Replacing a Fragment with another fragment after a asynctask

I have been spending so many hours on this, and I just want the quickest solution without altering my whole project. I have a Fragment1 that calls another fragment2 for data acquisition purposes. after fragment2 has acquired the data needed it then replace itself with fragment3 that shows all those data gathered. My fragment1 contain 2 fragments the search fragment and the other fragment is for the fragment2 and fragment3 mentioned earlier. what my fragment2 displays is a dialogbox and a textview that contains the error if the data is not acquired.

My problem is that I cannot somehow replace fragment2 with fragment3, it keeps on popping the error Null pointer exception on the FragmentTransaction ft = getFragmentManager().beginTransaction() line.. please help , Im new in android programming andif there is something that I missed out please do tell me.. thank you so much..

here is my code for fragment1

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

    final View rootView = inflater.inflate(R.layout.fragment_grade_layout, container, false);
     main_spnr = (Spinner) rootView.findViewById(R.id.spinner_gradecategory);
     main_spnr.setSelection(0);
     db = new mycsu_database(rootView.getContext());
     main_spnr.setOnItemSelectedListener(new OnItemSelectedListener() {
         @Override
         public void onItemSelected(AdapterView<?>arg0, View view, int arg2, long arg3) {
             Log.v(TAG, "gradecount= " + db.getgradecount(idnumber));
             if(db.getgradecount(idnumber)!=0){
                 Log.v(TAG, "gradecount= " + db.getgradecount(idnumber));
                 InterfaceMode = main_spnr.getSelectedItemPosition();
                 getfragments();
             }
             else{
                 Fragment fr = new DataGetterClass(idnumber,InterfaceMode);
                 FragmentManager fm = getFragmentManager();
                 FragmentTransaction fragmentTransaction = fm.beginTransaction();
                 fragmentTransaction.replace(R.id.frame_gradeListParent, fr);
                 fragmentTransaction.commit(); 
             }

         }
         @Override
         public void onNothingSelected(AdapterView<?> arg0) {
             // TODO Auto-generated method stub

         }
     });

     InterfaceMode = 0;
     prvInterfaceMode = -1;
     if(db.getgradecount(idnumber)!=0){
         Log.v(TAG, "gradecount= " + db.getgradecount(idnumber));
         InterfaceMode = main_spnr.getSelectedItemPosition();
         getfragments();
     }
     else{
         Fragment fr = new DataGetterClass(idnumber,InterfaceMode);
         FragmentManager fm = getFragmentManager();
         FragmentTransaction fragmentTransaction = fm.beginTransaction();
         fragmentTransaction.replace(R.id.frame_gradeListParent, fr);
         fragmentTransaction.commit(); 
     }
    return rootView;
}


private void getfragments(){

     Fragment fr = null;
     if(prvInterfaceMode != InterfaceMode){
         Log.v(TAG,"InterfaceMode: " + InterfaceMode);
         switch(InterfaceMode){

            case 0:
                Log.v(TAG,"here in 0");
                fr = new ListofGrades();break;
            case 1:
                fr = new fragment_selectsem();break;
            case 2:
                fr = new ListofallGrades();break;
            default:break;
         }

         if(fr!=null){
             Log.v(TAG,"calling " + InterfaceMode);
             FragmentManager fm = getFragmentManager();
             FragmentTransaction fragmentTransaction = fm.beginTransaction();
             fragmentTransaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
             fragmentTransaction.replace(R.id.frame_gradeListParent, fr);
             fragmentTransaction.commit(); 
         }
         prvInterfaceMode = InterfaceMode;
     }

}

my Fragment2 is the DataGetterClass()

public class DataGetterClass extends Fragment {

private static final String TAG = DataGetterClass.class.getSimpleName();
private static String _idnum="";
private static mycsu_database db;
private static ProgressBar progress;
private static TextView tvr;
private static int _callnxt;
FrameLayout frm;
int progressBarStatus = 0;
boolean done = false;

public DataGetterClass(String idnum, int callnxt){
    DataGetterClass._idnum = idnum;
    DataGetterClass._callnxt = callnxt;
}



@Override
public View onCreateView(LayoutInflater inflater,
          ViewGroup container, Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.fragment_loading, container, false);
    db = new mycsu_database(rootView.getContext());
    progress = (ProgressBar) rootView.findViewById(R.id.progressBar_getter);
    progress.setIndeterminate(true);
    tvr = (TextView) rootView.findViewById(R.id.textView1_error);
    frm = (FrameLayout) rootView.findViewById(R.id.frame_gradeListParent);
    tvr.setVisibility(View.INVISIBLE);
    new RecieveAsyncTask().execute();
    new Thread(new Runnable() {
        public void run() {
            while(true){
                if(done){
                    Log.v(TAG, "loading done");
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    Fragment wvf =  new ListofGrades();
                    ft.replace(frm.getId(), wvf);
                    ft.commit();
                    break;
                }

            }
        }
      }).start();

    return rootView;
}



private class  RecieveAsyncTask extends AsyncTask<Void, Void, Void> {

    String _Response;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progress.setVisibility(View.VISIBLE);
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        _Response = postData();

        return null;
    }


    @Override
    protected void onPostExecute(Void result) {  
        progress.setVisibility(View.INVISIBLE);
        if(_Response!=""){
            if(ParseJson(_Response))
                done=true;//getfragments();
            else
                tvr.setVisibility(View.VISIBLE);
        }
        else{
            Log.v("json", "unable to connect to server");
            tvr.setVisibility(View.VISIBLE);
        }
    }

}

public String postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://192.168.5.159/mycsu/mycsu.php");
    String _response = "";
    Log.v("trytry", " 11 ");
    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("studid", _idnum));
        nameValuePairs.add(new BasicNameValuePair("Mode","1"));
        nameValuePairs.add(new BasicNameValuePair("studyr",""));
        nameValuePairs.add(new BasicNameValuePair("studsem",""));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        InputStream inputStream = response.getEntity().getContent();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        StringBuilder stringBuilder = new StringBuilder();
        String bufferedStrChunk = null;

        while((bufferedStrChunk = bufferedReader.readLine()) != null){
            stringBuilder.append(bufferedStrChunk);
        }
        _response = stringBuilder.toString();
    } catch (ClientProtocolException e) {
        Log.d(TAG,e.toString());
        // TODO Auto-generated catch block
    } catch (IOException e) {
        Log.d(TAG,e.toString());
        // TODO Auto-generated catch block
    }
    //Log.v(TAG, _response);
    //Log.v("trytry", " 22 ");
   // Log.v("trytry",_response);
    return _response;
} 



boolean ParseJson(String JsonStr){
    boolean returnbool = true;
    String Jsontstr = "{ \"Android\":" + JsonStr + "}";
    GradeClass gc = new GradeClass();
    GpaClass gpac = new GpaClass();
    JSONObject jsonResponse;
    try{
        jsonResponse = new JSONObject(Jsontstr);
        JSONArray jsonMainNode = jsonResponse.optJSONArray("Android");
        int lengthJsonArr = jsonMainNode.length();

        for( int i=0; i< lengthJsonArr; i++){

            JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
            String coursecode = jsonChildNode.optString("courseno");
            String coursedesc = jsonChildNode.optString("description");
            String grade =jsonChildNode.optString("grade");
            int unit = Integer.parseInt(jsonChildNode.optString("unit"));
            String compgrade = jsonChildNode.optString("gcompl");
            String sem = jsonChildNode.optString("sem");
            String yr = jsonChildNode.optString("sy");
            String idnumber = jsonChildNode.optString("studid");
            float gpa =Float.parseFloat( jsonChildNode.optString("gpa"));
            String standing =  jsonChildNode.optString("standing");

            /*
            Log.d(TAG, "coursecode: " + coursecode  
                        +" coursedesc: " + coursedesc
                        +" grade: " + grade
                        +" unit: " + unit
                        +" compgrade: " + compgrade
                        +" sem: " + sem
                        +" yr: " + yr
                        +" idnumber: " + idnumber
                        +" gpa: " + gpa
                        +" standing: " + standing);
            */
            float compgradef, gradeint;
            if(grade.equals("INC"))
                gradeint = 4;
            else if(grade.equals("PASSED"))
                gradeint = (float) 4.5;
            else if(grade.equals("FAILED"))
                gradeint = (float) 6;
            else if(grade.equals("IN PROG"))
                gradeint = (float) 7;
            else
                gradeint =  Float.parseFloat(grade);

            if(compgrade.equals("null")){
                compgradef = 0;
            }else
                compgradef = Float.parseFloat(compgrade);




            gc.setcoursecode(coursecode);
            gc.setcoursedesc(coursedesc);
            gc.setgrade(gradeint);
            gc.setunits(unit);
            gc.setcompgrade(compgradef);
            gc.setcoursesem(sem);
            gc.setcourseyr(yr);
            gc.setidnumber(idnumber);
            gpac.setcoursesem(sem);
            gpac.setcourseyr(yr);
            gpac.setgpa(gpa);
            gpac.setidnumber(idnumber);
            gpac.setstanding(standing);



            if(db.findifgradeexist(gc)==false){
                db.add_grade(gc);
                //Log.v(TAG,"data GRADE inserted in db");
            }else;
                //Log.v(TAG,"data GRADE  exist NOT inserted in db");

            if(!db.findifgpaexist(gpac)==false){
                db.add_gpa(gpac);
                //Log.v(TAG,"data GPA inserted in db");
            }else;
                //Log.v(TAG,"data GPA exist NOT inserted in db");

        }


    }catch (JSONException e) {
        returnbool = false;
        e.printStackTrace();
    }
    return returnbool;
}

I did a lot of things in my fragment2 just for it to function the way I wanted too, to the point that I created a Thread that listens if the asynctask is done.. but again it doesnt work.. please do help if you have a solution in mind.. it will be appreciated..

oh!!, and here is the LogCat

 02-17 18:59:38.064: E/AndroidRuntime(3069): FATAL EXCEPTION: Thread-3298
02-17 18:59:38.064: E/AndroidRuntime(3069): java.lang.NullPointerException
02-17 18:59:38.064: E/AndroidRuntime(3069):     at com.csu.mycsu_proto1_DataGetter.DataGetterClass$1.run(DataGetterClass.java:82)
02-17 18:59:38.064: E/AndroidRuntime(3069):     at java.lang.Thread.run(Thread.java:856)

Upvotes: 0

Views: 808

Answers (1)

michael kagan
michael kagan

Reputation: 191

AyamSakata,

The issue here, I think, is your use of the local variable _Response in the AsyncTask. I don't believe that it is thread safe. doInBackground operates in the async thread, onPostExecute in the main thread. The AsyncTask class enables you to safely pass information between the threads by using the Void's in its header. The third Void is used to pass information from doInBackground to onPostExecute.

try changing the last two statements of doInBackground to

return postData();

and changing doInBackground to protected String

and changing the header to onPostExecute to

protected void onPostExecute(String _Response) {

and changing the header to ReceiveAsyncTask so that the third Void is changed to String (sorry, I can't input angle brackets in my answer or I would give the statement).

and eliminate the String _Response declaration.

Upvotes: 1

Related Questions