findViewById(int)方法未定义

时间:2015-07-31 03:41:13

标签: android facebook

我正在尝试创建一个连接到各种社交媒体(Facebook,Twitter等)并检索未读通知的应用程序

在我的主要活动中我有

public void retrieveFB(){

    FacebookReceiver fb = new FacebookReceiver();
    fb.loggedInCheck();

}

启动一个检查用户是否登录过的方法,如果他们这样做,只会显示一个facebook徽标。但如果他们不这样做,它将显示用户点击

的登录按钮视图

我尝试使用

将fbButtonView链接到xml中定义的login_button
fbButtonView = (LoginButton)findViewById(R.id.login_button);

但是错误

  

对于FacebookReceiver类型

,方法findViewById(int)未定义

我该如何解决这个问题?我在这里找到的大多数答案是用片段来膨胀视图,但我不确定是否应该使用片段。

这是我的完整代码

public class FacebookReceiver{

TextView fbCount;
LoginButton fbButtonView; //button

public FacebookReceiver()
{
    //constructor
}

public void loggedInCheck(){
    if(isLoggedIn() == null)
    {
        fbButtonView = (LoginButton)findViewById(R.id.login_button);//problem here 
        fbButtonView.setVisibility(View.VISIBLE);   
    }
    else
    {
        String FBCount = null;
        fbCount = (TextView).findViewById(R.id.facebookCount);//here too 

        FBCount = this.getFacebook();
        fbCount.setText(FBCount);
    }
}

private String getFacebook(){
    String FBCount = null;
     try
     {
        FBCount= new GraphRequest(AccessToken.getCurrentAccessToken(),
                            "/me/notifications",
                            null,
                            HttpMethod.GET,
                            new GraphRequest.Callback() {
                            public void onCompleted(GraphResponse response) {
                                /* handle the result */
                            }
                        }
                    ).executeAsync().toString();    

     }
     catch (Exception e)
     {
         e.printStackTrace();
     }
     return FBCount;
}

private AccessToken isLoggedIn(){
    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    return accessToken ;
}

}

2 个答案:

答案 0 :(得分:2)

首先,在FacebookReceiver内创建一个private Activity activity; public FacebookReceiver(Activity activity) { this.activity = activity; } 类,如下所示:

MainActivity

然后从FacebookReceiver类传递活动引用到FacebookReceiver fb = new FacebookReceiver(MainActivity.this); fb.loggedInCheck(); 类,如下所示:

loggedInCheck()

现在更新您的public void loggedInCheck(){ if(isLoggedIn() == null) { fbButtonView = (LoginButton) activity.findViewById(R.id.login_button); fbButtonView.setVisibility(View.VISIBLE); } else { String FBCount = null; fbCount = (TextView) activity.findViewById(R.id.facebookCount); FBCount = this.getFacebook(); fbCount.setText(FBCount); } } 方法,如下所示:

function sendMail($toAddress, $subject, $body, $AttachmentFilePath) {
$mail = new PHPMailer();

$mail->IsSMTP ();
$mail->CharSet = 'UTF-8';
// nable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0; // To prevent any outpur
// Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
// Get the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// Get the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
// Get the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
// Whether to use SMTP authentication
$mail->SMTPAuth = TRUE;

// Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "your.email@gmail.com";
// Password to use for SMTP authentication. Specific to the Lead Qualifier Tool
$mail->Password = "YourPassWordHere";

// Set who the message is to be sent from
$mail->SetFrom ( 'your.email@gmail.com', 'Your Name' );

// To Address
$mail->AddAddress ( $toAddress, $toAddress );

$mail->Subject = $subject;

$mail->Body = $body;
$mail->AltBody = $body;

// Add attachment
if ($AttachmentFilePath != NULL)
    $mail->AddAttachment ( $AttachmentFilePath );

if (! $mail->Send ()) {
    echo "<br />Error while sending e-mail: " . $mail->ErrorInfo;
} else {
    // echo "Message sent!";
}
}

答案 1 :(得分:0)

因为FacebookReceiver类没有扩展Activity类,所以findViewById方法不适用于此类。如果您的类设计为具有用户界面以便用户可以与其进行交互,则它必须是活动或片段。或者如果它仅用作后台服务来检查用户是否已登录,则它不必扩展活动或片段类。