Android Undefined getActivity错误

时间:2015-11-12 19:21:09

标签: android android-fragments android-activity

我正在按照教程开发Android聊天应用程序,但在

中收到错误(红线)

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

这两个和我的sdk版本是23,我是android的新手。我不知道该怎么办 ???请帮助我。

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();
        }

    }
}

}

4 个答案:

答案 0 :(得分:3)

导入错误FragmentFragmentTransaction,请替换您的导入:

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

v4Library

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

答案 1 :(得分:2)

除了已发布的内容之外,您还需要使用SupportFragmentManager代替FragmentManager

import android.support.v4.app.SupportFragmentManager;

更改此

FragmentTransaction ft = getFragmentManager().beginTransaction();

到这个

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

答案 2 :(得分:1)

尝试导入这些

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

答案 3 :(得分:1)

您需要上下文才能获得共享首选项,为此您可以使用ViewGroup container提供的上下文

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