0

无法解决以下代码中的错误:

     public class MainActivity extends Activity implements LoginListener{

    EditText et1;
     private FBLoginManager fbLoginManager;
     Button b;

     //replace it with your own Facebook App ID
     public final String FacebookTesting="218874471621782";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et1=(EditText)findViewById(R.id.editText1);
        b=(Button)findViewById(R.id.button1);
        connectToFacebook();

    }


     public void connectToFacebook(){

          //read about Facebook Permissions here:
          //http://developers.facebook.com/docs/reference/api/permissions/
          String permissions[] = {
    //          "user_about_me",
    //          "user_activities", 
    //          "user_birthday",
    //          "user_checkins",
    //          "user_education_history",
    //          "user_events",
    //          "user_groups", 
    //          "user_hometown",
     //         "user_interests",
    //          "user_likes",  
    //          "user_location",
    //          "user_notes",
    //          "user_online_presence",
    //          "user_photo_video_tags",
    //          "user_photos",
    //          "user_relationships",
    //          "user_relationship_details",
    //          "user_religion_politics",
    //              "user_status",
    //          "user_videos",
    //          "user_website",
    //          "user_work_history",
    //          "email",
    //
    //          "read_friendlists",
    //          "read_insights",
    //          "read_mailbox",
    //          "read_requests",
    //          "read_stream",
    //          "xmpp_login",
    //          "ads_management",
    //          "create_event",
    //          "manage_friendlists",
    //          "manage_notifications",
    //          "offline_access",
    //          "publish_checkins",
            "publish_stream",
    //          "rsvp_event",
    //          "sms",
            //"publish_actions",

    //          "manage_pages"

          };

          fbLoginManager = new FBLoginManager(this,
            R.layout.activity_main, 
            FacebookTesting, 
            permissions);

          if(fbLoginManager.existsSavedFacebook()){
           fbLoginManager.loadFacebook();
          }
          else{
           fbLoginManager.login();
          }
         }





    @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 onActivityResult(int requestCode, int resultCode,    android.content.Intentdata){


         fbLoginManager.loginSuccess(data);
     }

     public void loginSuccess(Facebook facebook) {
    //  GraphApi graphApi = new GraphApi(facebook);
         new getDataBackgroundThread().execute(facebook);
    //    User user = new User();
    //
    //    try{
    //     user = graphApi.getMyAccountInfo();
    //
    //     //update your status if logged in
    //     graphApi.setStatus("Hello, world!");
    //    } catch(EasyFacebookError e){
    //     Log.d("TAG: ", e.toString());
    //    }

    //    fbLoginManager.displayToast("Hey, " + user.getFirst_name() + "! Login success!");
     }

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

    }


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

    }



     class getDataBackgroundThread extends AsyncTask<Facebook, Void, String> {

            private Exception exception;


            protected String doInBackground(Facebook... urls) {
                try{
                     GraphApi graphApi = new GraphApi(urls[0]);

                      User user = new User();

                      try{
                    //   user = graphApi.getMyAccountInfo();

                       //update your status if logged in
                       //graphApi.setStatus("My android App "+"VivekAppTest");
                          graphApi.setStatus(et1+"VivekAppTest");
                      } catch(EasyFacebookError e){
                       Log.d("TAG: ", e.toString());
                      }
                return "posted";
                }
                catch (Exception e) {
                    // TODO: handle exception
                    return null;
                }


    }
            protected void onPostExecute(String ipr) {
                // TODO: check this.exception 
                // TODO: do something with the feed
     //
    //               Intent i=new Intent(getApplicationContext(),MainActivity.class);
    //               startActivity(i); 
    //               finish();
                Toast toast=Toast.makeText(getApplicationContext(),"Post Successful !", 1);
                toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL,0, 130);
                toast.show();

            }
        }



}

日志猫:

10-17 20:25:05.623: E/AndroidRuntime(401): FATAL EXCEPTION: main
10-17 20:25:05.623: E/AndroidRuntime(401): java.lang.IllegalStateException: Could not find a method (View) in the activity class com.example.facebooktesting.MainActivity for onClick handler on view class android.widget.Button with id 'button1'
10-17 20:25:05.623: E/AndroidRuntime(401):  at android.view.View$1.onClick(View.java:2059)
10-17 20:25:05.623: E/AndroidRuntime(401):  at android.view.View.performClick(View.java:2408)
10-17 20:25:05.623: E/AndroidRuntime(401):  at android.view.View$PerformClick.run(View.java:8816)
10-17 20:25:05.623: E/AndroidRuntime(401):  at android.os.Handler.handleCallback(Handler.java:587)
10-17 20:25:05.623: E/AndroidRuntime(401):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-17 20:25:05.623: E/AndroidRuntime(401):  at android.os.Looper.loop(Looper.java:123)
10-17 20:25:05.623: E/AndroidRuntime(401):  at android.app.ActivityThread.main(ActivityThread.java:4627)
10-17 20:25:05.623: E/AndroidRuntime(401):  at java.lang.reflect.Method.invokeNative(Native Method)
10-17 20:25:05.623: E/AndroidRuntime(401):  at java.lang.reflect.Method.invoke(Method.java:521)
10-17 20:25:05.623: E/AndroidRuntime(401):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-17 20:25:05.623: E/AndroidRuntime(401):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-17 20:25:05.623: E/AndroidRuntime(401):  at dalvik.system.NativeStart.main(Native Method)
10-17 20:25:05.623: E/AndroidRuntime(401): Caused by: java.lang.NoSuchMethodException: 
10-17 20:25:05.623: E/AndroidRuntime(401):  at java.lang.ClassCache.findMethodByName(ClassCache.java:308)
10-17 20:25:05.623: E/AndroidRuntime(401):  at java.lang.Class.getMethod(Class.java:985)
10-17 20:25:05.623: E/AndroidRuntime(401):  at android.view.View$1.onClick(View.java:2052)
10-17 20:25:05.623: E/AndroidRuntime(401):  ... 11 more
10-17 20:25:10.590: I/Process(401): Sending signal. PID: 401 SIG: 9
4

1 回答 1

0

如日志:

IllegalStateException:在活动类中找不到方法(视图)

意味着,您忘记在MainActivity已添加为回调的类中 button1定义方法作为activity_main.xml布局android:onClick。像在 MainActivity 中一样添加它:

public void button1_Click(View view) {
    // TODO Auto-generated method stub
    // on button1 click
}
于 2013-10-17T16:22:36.530 回答