我正在做这个教程:https://developers.facebook.com/docs/android/login-with-facebook/v2.2#step1
除了我将facebook登录添加到我当前的应用程序而不是创建一个新的空白。
我只是将oncreate代码粘贴到我的oncreate方法中。
按钮显示正常但我遇到的问题是我的mainactivity类中的oncreate方法的这一部分出现了Can not Resolve Method错误。有错误的区域是用***包裹的区域。
if (savedInstanceState == null) {
// Add the fragment on initial activity setup
mainFragment = new MainFragment();
getSupportFragmentManager()
.beginTransaction()
***.add(android.R.id.content, mainFragment)***
.commit();
} else {
// Or set the fragment from restored state info
mainFragment = ***(MainFragment) getSupportFragmentManager()
.findFragmentById(android.R.id.content);***
}
}
第一个错误:无法解析方法' add(packagename.mainFragment)
第二个错误:不可转换的类型:无法投射' android.support.v4.app.Fragment'到' package.mainFragment'
全班同学:
public class MainActivity extends FragmentActivity implements OnClickListener {
private MainFragment mainFragment;
Button sendIPbutton; //Button for sending IP Address
EditText mEdit; //Get info from what user enters in form
//TextView mText;
TextView coordinates;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*http://www.firstdroid.com/2010/04/29/android-development-using-gps-to-get-current-location-2/*/
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
/**********************************************************************/
/*get reference to views*/
sendIPbutton = (Button) findViewById(R.id.sendIP);
mEdit = (EditText) findViewById(R.id.enterIP);
sendIPbutton.setOnClickListener(this);
// InputMethodManager imm = (InputMethodManager)getSystemService(
// Context.INPUT_METHOD_SERVICE);
// imm.hideSoftInputFromWindow(mEdit.getWindowToken(), 0);
/*add click listener to Button "sendIPbutton"*/
// sendIPbutton.setOnClickListener(new OnClickListener() {
// @Override
// public void onClick(View arg0) {
// //Add JSON Logic here
//
String ip = "http://";
ip = ip + ((mEdit).getText().toString()) + "/rpi/";
System.out.println(ip);
//String json = "{\"lights\": [{\"lightId\": 1, \"red\":242,\"green\":116,\"blue\":12, \"intensity\": 0.5}],\"propagate\": true}";
InputStream inputStream = null;
String result = "";
try {
//1. CREATE HTTPCLIENT
HttpClient httpClient = new DefaultHttpClient();
System.out.println("no error");
//2. MAKE POST REQUEST TO GIVEN ipAddress
HttpPost httpPost = new HttpPost(ip);
System.out.println("no error2");
//String json = "";
//3. BUILD JSON OBJECT
//JSONObject jsonObject = new JSONObject();
//jsonObject.accumulate("")
String json = "";//"{\"lights\": [{\"lightId\": 1, \"red\":242,\"green\":116,\"blue\":12, \"intensity\": 0.5}],\"propagate\": true}";
JSONObject jsonObj = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");
json = jsonObj.toString();
httpPost.setEntity(new StringEntity(json));
//5. SET JSON to STRING ENTITY
// StringEntity se = new StringEntity(json);
// System.out.println("no error3");
//
// //6. SET HTTP POST ENTITY
// httpPost.setEntity(se);
// System.out.println("no error4");
//7. SET SOME HEADERS TO INFORM SERVER ABOUT THE TYPE OF CONTENT
// httpPost.setHeader("Accept", "application/json");
// System.out.println("no error5");
// httpPost.setHeader("Content-type", "application/json");
// System.out.println("no error6");
//HttpPost httpPost = new HttpPost(url);
//httpPost.setEntity(new StringEntity(json));
//CloseableHttpResponse response2 = httpclient.execute(httpPost);
//ERROR HERE
//8. EXECUTE POST REQUEST TO THE GIVEN IP ADDRESS
HttpResponse httpResponse = httpClient.execute(httpPost);
System.out.println("no error7");
// httpPost.completed();
// response2.close();
//9. RECEIVE RESPONSE AS inputStream
inputStream = httpResponse.getEntity().getContent();
System.out.println("no error8");
//10. CONVERT inputStream to string
if(inputStream != null) {
result = convertInputStreamToString(inputStream);
System.out.println("no error9");
}
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
if (savedInstanceState == null) {
// Add the fragment on initial activity setup
mainFragment = new MainFragment();
getSupportFragmentManager()
.beginTransaction()
.**add(android.R.id.content, mainFragment)**
.commit();
} else {
// Or set the fragment from restored state info
mainFragment = **(MainFragment) getSupportFragmentManager()
.findFragmentById(android.R.id.content);**
}
}
就这样你拥有它: 这是我的mainfragment.java代码
package uva.cs4720.ms3;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v4.app.*;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link MainFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link MainFragment#newInstance} factory method to
* create an instance of this fragment.
*
*/
public class MainFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment MainFragment.
*/
// TODO: Rename and change types and number of parameters
public static MainFragment newInstance(String param1, String param2) {
MainFragment fragment = new MainFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public MainFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
// @Override
// public View onCreateView(LayoutInflater inflater, ViewGroup container,
// Bundle savedInstanceState) {
// // Inflate the layout for this fragment
// return inflater.inflate(R.layout.fragment_main2, container, false);
// }
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
return view;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}
}
答案 0 :(得分:2)
怪异。在MainFragment中,只需删除import语句: import android.app.Fragment;
并添加此内容: import android.support.v4.app。*;
解决了它。
答案 1 :(得分:0)
问题是由于您从两个不同的类导入而导致的。因此不相容。
你有:
MainActivity - import android.support.v4.app.FragmentActivity;
MainFragment - import android.app.Fragment;
正确的方式:
MainActivity - import android.support.v4.app.FragmentActivity;
MainFragment - android.support.v4.app.Fragment;