Mayur Raikwar
Mayur Raikwar

Reputation: 105

Android Undefined getActivity error

i am following a tutorial to develop android chat app but getting error(red line) in

import android.app.Fragment; import android.app.FragmentTransaction;

these two and my sdk version is 23, i'm new to android. i don't know what to do ??? plz help me out.

package learn2crack.chat;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class LoginFragment extends Fragment {
SharedPreferences prefs;
EditText name, mobno;
Button login;
List<NameValuePair> params;
ProgressDialog progress;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.login_fragment, container, false);
    prefs = getActivity().getSharedPreferences("Chat", 0);

    name = (EditText)view.findViewById(R.id.name);
    mobno = (EditText)view.findViewById(R.id.mobno);
    login = (Button)view.findViewById(R.id.log_btn);
    progress = new ProgressDialog(getActivity());
    progress.setMessage("Registering ...");
    progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progress.setIndeterminate(true);



    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            progress.show();
            SharedPreferences.Editor edit = prefs.edit();
            edit.putString("REG_FROM", mobno.getText().toString());
            edit.putString("FROM_NAME", name.getText().toString());
            edit.commit();
            new Login().execute();
        }
    });

    return view;
}
private class Login extends AsyncTask<String, String, JSONObject> {

    @Override
    protected JSONObject doInBackground(String... args) {
        JSONParser json = new JSONParser();
        params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("name", name.getText().toString()));
        params.add(new BasicNameValuePair("mobno", mobno.getText().toString()));
        params.add((new BasicNameValuePair("reg_id",prefs.getString("REG_ID",""))));

        JSONObject jObj = json.getJSONFromUrl("http://10.0.2.2:8080/login",params);
        return jObj;



    }
    @Override
    protected void onPostExecute(JSONObject json) {
        progress.dismiss();
        try {
            String res = json.getString("response");
            if(res.equals("Sucessfully Registered")) {
                Fragment reg = new UserFragment();
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.content_frame, reg);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.addToBackStack(null);
                ft.commit();
            }else{
                Toast.makeText(getActivity(),res,Toast.LENGTH_SHORT).show();

            }

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

    }
}

}

Upvotes: 1

Views: 230

Answers (4)

Mohammed Aouf Zouag
Mohammed Aouf Zouag

Reputation: 17132

In addition to what's already posted, you need to use the SupportFragmentManager instead of FragmentManager.

import android.support.v4.app.SupportFragmentManager;

Change this

FragmentTransaction ft = getFragmentManager().beginTransaction();

to this

FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();

Upvotes: 2

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20626

You are importing wrong the Fragment and FragmentTransaction, replace your imports :

import android.app.Fragment;
import android.app.FragmentTransaction;

To v4Library :

import android.support.v4.app.Fragment
import android.support.v4.app.FragmentTransaction;

Upvotes: 3

Dalija Prasnikar
Dalija Prasnikar

Reputation: 28516

You need context in order to get shared preferences, to do that you can use context provided with ViewGroup container

prefs = container.getContext().getSharedPreferences("Chat", 0); 

Upvotes: 1

mfaisalhyder
mfaisalhyder

Reputation: 2284

Try to import these

import android.support.v4.app.Fragment
import android.support.v4.app.FragmentTransaction;

Upvotes: 1

Related Questions