質問

これは私のoncreate関数です。

onclicklistener内には多くのコードがあるので、こんにちは。どうやってやるの ?

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        facebook.authorize(this, permissions, Facebook.FORCE_DIALOG_AUTH,
                new DialogListener() {

                    public void onComplete(Bundle values) {
                    }

                    public void onFacebookError(FacebookError error) {
                    }

                    public void onError(DialogError e) {
                    }

                    public void onCancel() {

                    }
                });

        hello = (Button) findViewById(R.id.hello);
        info = (TextView) findViewById(R.id.facebook_info);
        content = (TextView) findViewById(R.id.content);

        hello.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                // bundle.putString("fields", "email");
                try {
                    about = facebook.request("me");
                    json = Util.parseJson(about);
                    id = json.getString("id");
                    first_name = json.getString("first_name");
                    last_name = json.getString("last_name");
                    email = json.getString("email");

                    if (!json.isNull("username")) {
                        username = json.getString("username");
                    } else {
                        Log.d("TAG", "Username not set");
                    }

                    dob = json.getString("birthday");
                    gender = json.getString("gender");
                    location = json.getString("location");

                    json_location = Util.parseJson(location);
                    place = json_location.getString("name");

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (FacebookError e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                // info.setText(about);
                content.setText(id + " \n" + first_name + " \n" + last_name
                        + " \n" + email + " \n" + username + " \n" + dob
                        + " \n" + gender + " \n" + place);
            }
        });

    }
役に立ちましたか?

解決

OnClickListenerを拡張する内部クラスを作成します。

private class MyOnClickListener implements OnClickListener { ... }

ここでは、匿名のonClickListenerにすべての準備ができているのとまったく同じ実装を追加します。アクセスするすべての変数がアクティビティクラスのローカル変数として宣言されていることを確認してください。

次に、OnClickListenerを設定すると、これを行うだけです。

hello.setOnClickListener( new MyOnCLickListener() );

他のヒント

 public class ClassName extends Activity implements OnClickListener {

    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            facebook.authorize(this, permissions, Facebook.FORCE_DIALOG_AUTH,
                    new DialogListener() {

                        public void onComplete(Bundle values) {
                        }

                        public void onFacebookError(FacebookError error) {
                        }

                        public void onError(DialogError e) {
                        }

                        public void onCancel() {

                        }
                    });

            hello = (Button) findViewById(R.id.hello);
            hello.setOnClickListener(this);
            info = (TextView) findViewById(R.id.facebook_info);
            content = (TextView) findViewById(R.id.content);

        }

    public void onClick(View arg0) {
        if(arg0==hello){
                    try {
                        about = facebook.request("me");
                        json = Util.parseJson(about);
                        id = json.getString("id");
                        first_name = json.getString("first_name");
                        last_name = json.getString("last_name");
                        email = json.getString("email");

                        if (!json.isNull("username")) {
                            username = json.getString("username");
                        } else {
                            Log.d("TAG", "Username not set");
                        }

                        dob = json.getString("birthday");
                        gender = json.getString("gender");
                        location = json.getString("location");

                        json_location = Util.parseJson(location);
                        place = json_location.getString("name");

                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (FacebookError e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    // info.setText(about);
                    content.setText(id + " \n" + first_name + " \n" + last_name
                            + " \n" + email + " \n" + username + " \n" + dob
                            + " \n" + gender + " \n" + place);
                }
         }

    }

コードを交換し、そのために上記のコードを書き込みます。

コードを公開 /保護された関数に配置します。 OnClickListenerはあなたの内部の抽象クラスであるため、それをネストするクラスからパブリックまたは保護された方法にアクセスできます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top