显示进度条时出错

时间:2016-05-25 16:37:50

标签: android progress-bar android-progressbar

我正在尝试显示加载进度条,同时向我的服务器发送请求以从facebook图形api注册用户数据。但是我有一个错误。“java.lang.IllegalStateException:指定的子项已经有父项。必须首先在孩子的父母身上调用removeView()“。请你看看这个。 Login.java

function theDifference(){
    for(var emre = 0; emre < 10; emre++){
    // emre is visible inside of this for()
    }

// emre is visible here too.
}

progressbar.xml

package fixmystreet.teamyolo.net.pyinpaypar;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import com.securepreferences.SecurePreferences;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Arrays;
import java.util.HashMap;

import fixmystreet.teamyolo.net.pyinpaypar.Api.LoginApi;
import fixmystreet.teamyolo.net.pyinpaypar.Api.MainService;
import fixmystreet.teamyolo.net.pyinpaypar.Utilties.FinalResult;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Login extends AppCompatActivity {

    private TextView greetTxt;
    private LoginButton loginButton;
    private CallbackManager callbackManager;
    public String user_id;
    public String user_name;
    private SecurePreferences sharedPreferences;

    private String mail;
    ProgressBar myProgressBar;
    AlertDialog.Builder myDialogBuilder;


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
        setContentView(R.layout.activity_login);

        loginButton = (LoginButton) findViewById(R.id.login_button);
        greetTxt = (TextView) findViewById(R.id.info);
        greetTxt.setTypeface(MainApplication.typefaceManager.getUnicode());

        loginButton.setReadPermissions(Arrays.asList("public_profile, email"));

        myDialogBuilder = new AlertDialog.Builder(this);
        myDialogBuilder.setTitle("loading");

        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {

                GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(
                                    JSONObject object,
                                    GraphResponse response) {

                                try {
                                    user_name = response.getJSONObject().get("name") + "";
                                    user_id = response.getJSONObject().get("id") + "";
                                    mail = response.getJSONObject().get("email") + "";
                                    Toast.makeText(Login.this, "Logging in.Please wait!",
                                            Toast.LENGTH_SHORT).show();
                                    sharedPreferences = new SecurePreferences(getApplicationContext());
                                    SharedPreferences.Editor editor = sharedPreferences.edit();
                                    editor.putString("id", user_id);
                                    editor.putString("name",user_name);
                                    editor.commit();

                                    MainApplication.securedUserId=user_id;
                                    MainApplication.securedUserName=user_name;


                                    Log.i("Before circular loading","Before circular loading");

                                    //for circular loading
                                    View ProgressView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.progressbar,null);
                                    myProgressBar=(ProgressBar)ProgressView.findViewById(R.id.ProgressBar);

                                    myDialogBuilder.setView(myProgressBar);
                                    //myProgressBar.getParent().removeView();
                                    myDialogBuilder.create().show();


                                    register(user_id);

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                                try {
                                    response.getJSONObject().get("id");
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,email,gender, birthday");
                request.setParameters(parameters);
                request.executeAsync();
            }

            @Override
            public void onCancel() {
                Log.i("On Cancel", "On Cancel");
            }

            @Override
            public void onError(FacebookException error) {
                Log.i("ggwp", error.getMessage());
                Toast.makeText(Login.this, "Error in facebook login",
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void setProfile() {
        getFacebookProfilePicture(user_id + "");
    }

    public void getFacebookProfilePicture(String userID) {

        try {
            String imageUrl = "https://graph.facebook.com/" + userID + "/picture?type=large";
        } catch (Exception e) {
            Log.e("ReadRdaJSONFeedTask", e.getLocalizedMessage() == null ? "ERROR IS NULL" : "ERROR IS NOT NULL AND IT IS:" + e.getLocalizedMessage());
        }

    }

    public void register(String id) {


        Toast.makeText(Login.this, "Logging in.Please wait!",
                Toast.LENGTH_SHORT).show();
        String pic_link = "https://graph.facebook.com/" + id + "/picture?type=large";
        Call<HashMap<String, String>> register = LoginApi.createService(MainService.class).register(id, user_name, pic_link, mail);
        Toast.makeText(Login.this, "Logging in.Please wait!",
                Toast.LENGTH_SHORT).show();
        register.enqueue(new Callback<HashMap<String, String>>() {
            @Override
            public void onResponse(Call<HashMap<String, String>> call, Response<HashMap<String, String>> response) {
                Toast.makeText(Login.this, "Success Login!",
                        Toast.LENGTH_SHORT).show();
                Log.i("SUCCESS:", "MOTHER FUCKER");
                Log.i("Server Message:", response.message().toString());
                FinalResult.isLoggedIn = true;
                Intent i = new Intent(Login.this, MainActivity.class);
                startActivity(i);
            }

            @Override
            public void onFailure(Call<HashMap<String, String>> call, Throwable t) {
                Toast.makeText(Login.this, "Facebook Login Fail!",
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

}

这是我的日志

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ProgressBar
        android:id="@+id/ProgressBar"
        style="?android:attr/progressBarStyleLarge"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />


</RelativeLayout>

这是第112行Login.java

05-25 20:52:43.712 2799-2855/fixmystreet.teamyolo.net.pyinpaypar E/chromium: [ERROR:buffer_manager.cc(313)] [.Parent-Compositor-0xe2b09a90]GL ERROR :GL_INVALID_OPERATION : glBufferData: <- error from previous GL command
05-25 20:54:14.892 2799-2799/fixmystreet.teamyolo.net.pyinpaypar E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                   Process: fixmystreet.teamyolo.net.pyinpaypar, PID: 2799
                                                                                   java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
                                                                                       at android.view.ViewGroup.addViewInner(ViewGroup.java:3880)
                                                                                       at android.view.ViewGroup.addView(ViewGroup.java:3733)
                                                                                       at android.view.ViewGroup.addView(ViewGroup.java:3709)
                                                                                       at android.support.v7.app.AlertController.setupCustomContent(AlertController.java:610)
                                                                                       at android.support.v7.app.AlertController.setupView(AlertController.java:449)
                                                                                       at android.support.v7.app.AlertController.installContent(AlertController.java:214)
                                                                                       at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:240)
                                                                                       at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
                                                                                       at android.app.Dialog.show(Dialog.java:274)
                                                                                       at fixmystreet.teamyolo.net.pyinpaypar.Login$1$1.onCompleted(Login.java:112)
                                                                                       at com.facebook.GraphRequest$1.onCompleted(GraphRequest.java:304)
                                                                                       at com.facebook.GraphRequest$5.run(GraphRequest.java:1379)
                                                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                       at android.os.Looper.loop(Looper.java:135)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                                       at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

1 个答案:

答案 0 :(得分:1)

您应该尝试将视图设置为整个布局,而不是其中的单个元素。试着这样做:

View ProgressView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.progressbar,null);
myDialogBuilder.setView(ProgressView);
myDialogBuilder.create().show();

与此特定问题无关的另一条建议:

  

遵循Java中的命名约定;方法和变量在开头使用 camelCase 命名为小写字母;所以在你的情况下,它将是progressView,类总是以大写字母开头。