user3818495
user3818495

Reputation: 47

Android - About fragment

Because I have do about slide menu, it's need to "extends Fragment", I have read:Fragment can't include Activity, but my sourcecode " Toast.makeText(getApplicationContext(), "Error..." + e.toString(), Toast.LENGTH_LONG).show();" ,'getApplicationContext()' will some wrong, I don't know how to solve this proble. Is have anyone can help me????

thanks a lot.

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("NewApi")
public class FB_Fragment extends Fragment {
    private String jsonResult;
    private ListView listView;
    String outPut;


public View onCreateView(LayoutInflater inflater, ViewGroup container,    Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View view = inflater.inflate(R.layout.fb_fragment, container, false);
    listView = (ListView) view.findViewById(R.id.listView1);    

    return view;
}
private class JsonReadTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(params[0]);
        try {
            HttpResponse response = httpclient.execute(httppost);
            jsonResult = inputStreamToString(
                    response.getEntity().getContent()).toString();
        }

        catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }




    private StringBuilder inputStreamToString(InputStream is) {
        String rLine = "";
        StringBuilder answer = new StringBuilder();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));

        try {
            while ((rLine = rd.readLine()) != null) {
                answer.append(rLine);
            }
        }

        catch (IOException e) {
            // e.printStackTrace();
            Toast.makeText(getApplicationContext(),
                    "Error..." + e.toString(), Toast.LENGTH_LONG).show();
        }
        return answer;
    }
}

}

Upvotes: 1

Views: 81

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

getApplicationContext() method is not available in Fragment class, use getActivity() instead of getApplicationContext() for getting Context:

Toast.makeText(getActivity(),
                    "Error..." + e.toString(), Toast.LENGTH_LONG).show();

or call getApplicationContext method as: getActivity().getApplicationContext()

Upvotes: 1

Related Questions