gpt4 book ai didi

java - findFragmentByID 不起作用,并且 showFragment 不是有效方法

转载 作者:行者123 更新时间:2023-12-01 11:45:17 25 4
gpt4 key购买 nike

我正在尝试使用本指南构建 Facebook 登录:https://developers.facebook.com/docs/android/scrumptious/authenticate ,并且我在 showFragment (这甚至不是一个东西)和 findFragmentByID 上收到错误,这表明我指定的 fragment 无效(尝试 fragment 类和 fragment 布局不起作用)。有什么帮助吗?代码:

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.Toast;

import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;

import static com.eren.valour.R.id.*;

public class MainScreen extends ActionBarActivity {
private static final int FBSPLASH = 0;
private static final int FBSELECTION = 1;
private static final int FBFRAGMENT_COUNT = FBSELECTION + 1;
private Fragment[] fragments = new Fragment[FBFRAGMENT_COUNT];
ImageButton fblogin;
Session session = Session.getActiveSession();
private boolean isResumed = false;
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback =
new Session.StatusCallback() {
@Override
public void call(Session session,
SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayShowCustomEnabled(true);
setTitle("Valour");
setContentView(R.layout.activity_main_screen);

FragmentManager fm = getSupportFragmentManager();
fragments[FBSPLASH] = fm.findFragmentById(R.id.fragment_facebook_login_dialogue);
fragments[FBSELECTION] = fm.findFragmentById(R.id.FacebookLoginSelectFragment);

FragmentTransaction transaction = fm.beginTransaction();
for (int i = 0; i < fragments.length; i++) {
transaction.hide(fragments[i]);
}
transaction.commit();
getScreenRes();
}

@Override
public void onResume() {
super.onResume();
isResumed = true;
}

@Override
public void onPause() {
super.onPause();
isResumed = false;
}

public void getScreenRes() {
DisplayMetrics display = this.getResources().getDisplayMetrics();
int screenwidth = display.widthPixels;
double buttonheight = screenwidth / 2.66666667;
int screenheight = (int) Math.round(buttonheight);

// ImageButton serviceList = (ImageButton) findViewById(R.id.addservice);
// ViewGroup.LayoutParams servicelist = serviceList.getLayoutParams();
// servicelist.width = screenwidth;
// servicelist.height = screenheight;


//Get screen dimensions and define button variables
ImageButton redditLogin = (ImageButton) findViewById(R.id.redditLogin);
ViewGroup.LayoutParams reddit = redditLogin.getLayoutParams();
reddit.width = screenwidth;
reddit.height = screenheight;

ImageButton fbLogin = (ImageButton) findViewById(R.id.facebookLogin);
ViewGroup.LayoutParams fb = fbLogin.getLayoutParams();
fb.width = screenwidth;
fb.height = screenheight;


ImageButton instaLogin = (ImageButton) findViewById(R.id.instagramLogin);
ViewGroup.LayoutParams insta = instaLogin.getLayoutParams();
insta.width = screenwidth;
insta.height = screenheight;

ImageButton twitLogin = (ImageButton) findViewById(R.id.twitterLogin);
ViewGroup.LayoutParams twit = twitLogin.getLayoutParams();
twit.width = screenwidth;
twit.height = screenheight;

// set button size
instaLogin.setLayoutParams(insta);
fbLogin.setLayoutParams(fb);
twitLogin.setLayoutParams(twit);
redditLogin.setLayoutParams(reddit);
}

public void facebookLogin(View v) {

if (session == null) {
session = new Session(getApplicationContext());
}
Session.setActiveSession(session);

}

public void instagramLogin(View v) {
serviceIncomplete();


}

public void redditLogin(View v) {
serviceIncomplete();


}

public void twitterLogin(View v) {
serviceIncomplete();


}

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
// Only make changes if the activity is visible
if (isResumed) {
FragmentManager manager = getSupportFragmentManager();
// Get the number of entries in the back stack
int backStackSize = manager.getBackStackEntryCount();
// Clear the back stack
for (int i = 0; i < backStackSize; i++) {
manager.popBackStack();
}
if (state.isOpened()) {
// If the session state is open:
// Show the authenticated fragment
showFragment(FBSELECTION, false);
} else if (state.isClosed()) {
// If the session state is closed:
// Show the login fragment
showFragment(FBSPLASH, false);
}
}
}

@Override
protected void onResumeFragments() {
super.onResumeFragments();
Session session = Session.getActiveSession();

if (session != null && session.isOpened()) {
// if the session is already open,
// try to show the selection fragment
showFragment(FBSELECTION, false);
} else {
// otherwise present the splash screen
// and ask the person to login.
showFragment(FBSPLASH, false);
}
}

最佳答案

您第二次调用 findFragmentById 没有找到任何内容,因为您的参数是 R.id。

您需要提供有效的 ID 才能找到您的 fragment 。

关于java - findFragmentByID 不起作用,并且 showFragment 不是有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29192103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com