无法解析方法Add(*:Facebook的登录到Android应用程序 [英] Cannot resolve method add(*.: Facebook Login to Android App
问题描述
我做这个教程: https://开头developers.facebook.com/docs/android/login-with-facebook/v2.2#step1
除了我加入Facebook登录以我目前的应用程序,而不是创建一个新的空白之一。
我刚刚粘贴的OnCreate code到我的onCreate方法。
按钮显示了罚款,但我遇到的问题是,我得到一个不能解析的方法错误我的onCreate方法在我MainActivity类别本节。与错误的领域是包裹着的那些***。
如果(savedInstanceState == NULL){
//添加片段初始设置活动
mainFragment =新MainFragment();
getSupportFragmentManager()
.beginTransaction()
***添加(android.R.id.content,mainFragment)***
。承诺();
}其他{
//或者设置为从恢复状态信息的片段
mainFragment = ***(MainFragment)getSupportFragmentManager()
.findFragmentById(android.R.id.content); ***
} }
第一个错误:无法解析法添加(packagename.mainFragment)
第二个错误:不可兑换类型:无法施展'android.support.v4.app.Fragment'到'package.mainFragment
下面是整个类:
公共类MainActivity扩展FragmentActivity实现OnClickListener {
私人MainFragment mainFragment;
按钮sendIPbutton; //按钮发送IP地址
的EditText mEdit; //从哪些用户获取信息形式进入
// TextView的多行文字;
TextView的坐标;
@覆盖
保护无效的onCreate(捆绑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/*/
/ *使用的LocationManager类获取GPS位置* /
的LocationManager mlocManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener的mlocListener =新MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,mlocListener);/ ******************* ********************* /
/ *获取参考意见* /
sendIPbutton =(按钮)findViewById(R.id.sendIP);
mEdit =(EditText上)findViewById(R.id.enterIP);
sendIPbutton.setOnClickListener(本);// InputMethodManager IMM =(InputMethodManager)getSystemService(
// Context.INPUT_METHOD_SERVICE);
// imm.hideSoftInputFromWindow(mEdit.getWindowToken(),0);
/ *点击监听器添加到按钮sendIPbutton* /
// sendIPbutton.setOnClickListener(新OnClickListener(){
// @覆盖
//公共无效的onClick(查看为arg0){
// //添加JSON这里的逻辑
//
串IP =HTTP://;
IP = IP +((mEdit).getText()的toString())+/ RPI /;
的System.out.println(IP);
// JSON字符串={\\灯\\:[{\\lightId \\:1,\\红\\:242,\\绿色\\116 \\蓝\\:12 \\的力度\\:0.5}],\\传播\\:真正}; 为InputStream的InputStream = NULL;
字符串结果=;
尝试{
// 1。 CREATE的HttpClient
HttpClient的HttpClient的=新DefaultHttpClient();
的System.out.println(无差错);
// 2。 MAKE POST请求给定的ip地址
HttpPost httpPost =新HttpPost(IP);
的System.out.println(无误差2); // JSON字符串=;
// 3。 BUILD JSON对象
//的JSONObject的JSONObject =新的JSONObject();
//jsonObject.accumulate()
JSON字符串=; //{\\灯\\:[{\\lightId \\:1,\\红\\:242,\\绿色\\116 \\蓝\\:12, \\强\\:0.5}],\\传播\\:真正};
JSONObject的jsonObj =新的JSONObject({\\PHONETYPE \\:\\N95 \\,\\猫\\:\\WP \\});
JSON = jsonObj.toString();
httpPost.setEntity(新StringEntity(JSON)); // 5。 SET JSON来STRING实体
// StringEntity SE =新StringEntity(JSON);
//的System.out.println(无误差3);
//
// // 6。 SET HTTP POST实体
// httpPost.setEntity(SE);
//的System.out.println(无error4); // 7。设置一些标头INFORM Server中的内容的类型
// httpPost.setHeader(接受,应用/ JSON);
//的System.out.println(无error5);
// httpPost.setHeader(内容类型,应用/ JSON);
//的System.out.println(无error6);
// HttpPost httpPost =新HttpPost(URL);
//httpPost.setEntity(new StringEntity(JSON));
// CloseableHtt presponse响应2 = httpclient.execute(httpPost);
// ERROR HERE
// 8。 EXECUTE POST请求指定IP地址
HTT presponse HTT presponse = httpClient.execute(httpPost); 的System.out.println(无error7);
// httpPost.completed();
// response2.close(); // 9。接收响应,为InputStream
的InputStream = HTT presponse.getEntity()的getContent()。
的System.out.println(无error8);
// 10。 CONVERT的InputStream串
如果(的InputStream!= NULL){
结果= convertInputStreamToString(InputStream的);
的System.out.println(无error9);
}
其他
结果=没有工作! }赶上(例外五){ Log.d(InputStream的,e.getLocalizedMessage());
}如果(savedInstanceState == NULL){
//添加片段初始设置活动
mainFragment =新MainFragment();
getSupportFragmentManager()
.beginTransaction()
**加(android.R.id.content,mainFragment)**
。承诺();
}其他{
//或者设置为从恢复状态信息的片段
mainFragment = **(MainFragment)getSupportFragmentManager()
.findFragmentById(android.R.id.content)**
} }
和只是让你知道了:
这里是我的mainfragment.java code
包uva.cs4720.ms3;进口android.app.Activity;
进口android.net.Uri;
进口android.os.Bundle;
进口android.app.Fragment;
导入android.support.v4.app *。
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;/ **
*一个简单的{@link片段}子类。
*包含此片段必须实现活动
* {@link MainFragment.OnFragmentInteractionListener}接口
*处理交互事件。
*使用{@link MainFragment#}的newInstance工厂方法
*创建该片段的一个实例。
*
* /
公共类MainFragment扩展片段{
// TODO:重命名参数的参数,选择匹配的名字
//片段初始化参数,例如ARG_ITEM_NUMBER
私有静态最后弦乐ARG_PARAM1 =参数1;
私有静态最后弦乐ARG_PARAM2 =参数2; // TODO:重命名和更改类型的参数
私人字符串mParam1;
私人字符串mParam2; 私人OnFragmentInteractionListener mListener; / **
*使用此工厂方法来创建新实例
*使用提供的参数,这个片段。
*
* @参数参数1参数1。
* @参数参数2参数2。
返回:片段MainFragment的一个新实例。
* /
// TODO:重命名和更改类型和参数个数
公共静态的newInstance MainFragment(字符串参数1,字符串参数2){
MainFragment片段=新MainFragment();
捆绑ARGS =新包();
args.putString(ARG_PARAM1,参数1);
args.putString(ARG_PARAM2,参数2);
fragment.setArguments(参数);
返回片段;
}
公共MainFragment(){
//必需的空公共构造
} @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
如果(getArguments()!= NULL){
mParam1 = getArguments()的getString(ARG_PARAM1)。
mParam2 = getArguments()的getString(ARG_PARAM2)。
}
}// @覆盖
//公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
//捆绑savedInstanceState){
// //充气的布局该片段
//返回inflater.inflate(R.layout.fragment_main2,集装箱,FALSE);
//} @覆盖
公共查看onCreateView(LayoutInflater气筒,
ViewGroup中的容器中,
捆绑savedInstanceState){
查看查看= inflater.inflate(R.layout.fragment_main,集装箱,FALSE); 返回视图。
}
// TODO:重命名方法,更新参数和勾法将UI事件
公共无效onButton pressed(URI URI){
如果(mListener!= NULL){
mListener.onFragmentInteraction(URI);
}
} @覆盖
公共无效onAttach(活动活动){
super.onAttach(活动);
尝试{
mListener =(OnFragmentInteractionListener)活性;
}赶上(抛出ClassCastException E){
抛出新ClassCastException异常(activity.toString()
+必须实现OnFragmentInteractionListener);
}
} @覆盖
公共无效onDetach(){
super.onDetach();
mListener = NULL;
} / **
*此接口必须由包含此活动的实施
*片段,以允许在该片段的相互作用来传达
*中包含的活性和潜在的其它片段
*活动。
*所述p为H.;
*请参阅Android的培训课< A HREF =
*http://developer.android.com/training/basics/fragments/communicating.html
*>在其他片段&所述通信; / A>了解更多信息。
* /
公共接口OnFragmentInteractionListener {
// TODO:更新参数类型和名称
公共无效onFragmentInteraction(URI URI);
}}
奇怪。在MainFragment,只是去掉import语句:
进口android.app.Fragment;
和添加这样的:
进口android.support.v4.app *;
解决了这个问题。
I am doing this tutorial:https://developers.facebook.com/docs/android/login-with-facebook/v2.2#step1
Except I am adding the facebook login to my current app instead of creating a new blank one.
I've just pasted the oncreate code into my oncreate method.
The button shows up fine but the issue I am having is that I am getting a Cannot Resolve Method error for this section of my oncreate method in my mainactivity class. The areas with the error are the ones wrapped with the ***.
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);***
}
}
First Error: Cannot resolve method 'add(packagename.mainFragment)
Second error: Inconvertible types: cannot cast 'android.support.v4.app.Fragment' to 'package.mainFragment'
Here's the whole class:
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);**
}
}
And just so you have it: Here's my mainfragment.java code
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);
}
}
Weird. In MainFragment, just removing the import statement: import android.app.Fragment;
and adding this: import android.support.v4.app.*;
solved it.
这篇关于无法解析方法Add(*:Facebook的登录到Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!