我正在使用Facebook SDK,我正面临一个问题。 FacebookLoginFragment覆盖了我的登录活动。事实上,我知道答案的原因,但我现在正在努力解决问题。
我需要onCreateView方法中的视图,但如果我给它充气,它只是我的activity_login的叠加层。我怎样才能在不膨胀的情况下获得正确的视图呢?
请参阅以下代码。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//get editText Views
editTextlogin = (EditText) findViewById(R.id.editText_login);
editTextpasswd = (EditText) findViewById(R.id.editText_passwd);
if (savedInstanceState == null) {
// add the fragment on initial activity setup
mainFragment = new FacebookLoginFragment();
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, mainFragment).commit();
} else {
// or set the fragment from restored state info
mainFragment = (FacebookLoginFragment) getSupportFragmentManager()
.findFragmentById(android.R.id.content);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
con = new FacebookConnection();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_login, container, false);
// The results of the authentication are returned to the activity, to
// get the Fragment to receive it
// call the setFragment() method
LoginButton authButton = (LoginButton) view
.findViewById(R.id.authButton);
authButton.setFragment(this);
if (Session.getActiveSession().isClosed()) {
authButton.setReadPermissions("user_status", "email", "publish_actions");
}
return view;
}
(我知道相对布局不是最佳实践,只是用于测试)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="at.ts_company.activity.Login" >
<View
android:id="@+id/view1"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_centerInParent="true"
android:background="@color/primary_material_light" />
<EditText
android:id="@+id/editText_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="108dp"
android:ems="10"
android:hint="@string/login_loginname"
android:inputType="text" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText_passwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText_login"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="@string/login_passwd"
android:inputType="textPassword"/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:contentDescription="@string/logo"
android:scaleType="fitCenter"
android:src="@drawable/final_logo" />
<TextView
android:id="@+id/textView_createAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="68dp"
android:text="@string/login_createAccount"
android:clickable="true"
android:onClick="register"/>
<Button
android:id="@+id/button_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText_passwd"
android:layout_centerHorizontal="true"
android:drawableLeft="@drawable/arrow_right"
android:text="@string/login_loginbutton"
android:onClick="login"/>
<com.facebook.widget.LoginButton
android:id="@+id/authButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView_createAccount"
android:layout_alignLeft="@+id/editText_passwd"
android:layout_marginBottom="40dp" />
答案 0 :(得分:1)
好吧,我必须回答我自己的问题。
这并不像我想象的那么容易,但这并不是我想到的解决方案。
我已在Fragment(FacebookLoginFragment)中添加了功能,而在Login.java中我只调用了Fragment。
我必须删除Login中的setContentView(R.layout.activity_login)
,并将除if语句之外的所有功能添加到Fragment中。
我在Login.xml中为我的按钮定义了onClick方法,因此我的Login.java中有一个方法login(View view)
,只调用fragment.login(View view)
答案 1 :(得分:0)
也许有点晚了,但为什么不通过静态实例访问呢?
private static MyActivity Instance;
public static MyActivity getInstance() { return Instance; }
在onCreate
方法中,您将活动分配给实例Instance = this;
但请务必检查返回的实例是否不是null
。