2

我在将代码转换为片段时遇到问题

这是我的应用程序仍然是活动时的代码

package com.ronnielp.loginsample2;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.plus.PlusClient;
import com.google.android.gms.plus.model.people.Person;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class SignIn extends Activity implements OnClickListener, 
    ConnectionCallbacks, OnConnectionFailedListener{


    private static final int REQUEST_CODE_RESOLVE_ERR = 40;
    private PlusClient mPlusClient;
    private ConnectionResult mConnectionResult;
    private TextView txtUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mPlusClient =new PlusClient.Builder(this, this, this)
        .setScopes(Scopes.PLUS_LOGIN)
        .setVisibleActivities("http://schemas.google.com/AddActivity")
        .build();

        findViewById(R.id.sign_in_button).setOnClickListener(this);
        txtUser = (TextView) findViewById(R.id.txtUser);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        mPlusClient.connect();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        mPlusClient.disconnect();
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        // TODO Auto-generated method stub
        mConnectionResult = result;
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        // TODO Auto-generated method stub
        Person user = mPlusClient.getCurrentPerson();
        String acc = mPlusClient.getAccountName();
        txtUser.setText(acc);
    }

    @Override
    public void onDisconnected() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub
        if (view.getId() == R.id.sign_in_button &&  !mPlusClient.isConnected() && mConnectionResult !=null){
            try{
                mConnectionResult.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
            } catch (SendIntentException e){
                mConnectionResult = null;
                mPlusClient.connect();
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (requestCode == REQUEST_CODE_RESOLVE_ERR && resultCode == RESULT_OK){
            mConnectionResult = null;
            mPlusClient.connect();
        }
    }


}

当我将其转换为片段时,我的代码不再起作用请帮助我,我仍然是 Android 编程的新手

4

1 回答 1

3

事情的数量可能出错了。一个问题是访问您的 childFragmentManager。管理器不会将结果传递给片段,您必须在基类中手动执行此操作。

 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
      super.onActivityResult(requestCode, resultCode, intent);
      Fragment fragment = (Fragment) getChildFragmentManager().findFragmentByTag(childTag);
      if(fragment != null){
            fragment.onActivityResult(requestCode, resultCode, intent);
      }
 }
于 2013-07-19T08:52:41.277 回答