我正在创建分享到Facebook,Twitter和其他社交网络的应用程序。我的源代码中没有任何错误,但是当我在模拟器按钮中运行应用程序时,onClickListener无法正常工作。这是我的代码
public class MainActivity extends Activity {
SocialAuthAdapter adapter;
Profile profileMap;
List<Photo> photosList;
Button update;
EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textview = (TextView) findViewById(R.id.text);
textview.setText("Welcome to Share Application experiment");
Button share = (Button) findViewById(R.id.sharebutton);
share.setText("Share");
share.setTextColor(Color.WHITE);
share.setBackgroundResource(R.drawable.button_gradient);
adapter = new SocialAuthAdapter(new ResponseListener());
adapter.addProvider(Provider.FACEBOOK, R.drawable.facebook);
adapter.addProvider(Provider.TWITTER, R.drawable.twitter);
adapter.addProvider(Provider.LINKEDIN, R.drawable.linkedin);
adapter.addProvider(Provider.MYSPACE, R.drawable.myspace);
adapter.addProvider(Provider.YAHOO, R.drawable.yahoo);
adapter.addProvider(Provider.YAMMER, R.drawable.yammer);
adapter.addProvider(Provider.FOURSQUARE, R.drawable.foursquare);
adapter.addProvider(Provider.GOOGLE, R.drawable.google);
adapter.addProvider(Provider.SALESFORCE, R.drawable.salesforce);
adapter.addProvider(Provider.RUNKEEPER, R.drawable.runkeeper);
adapter.addCallBack(Provider.FOURSQUARE, "http://socialauth.in/socialauthdemo/socialAuthSuccessAction.do");
adapter.addCallBack(Provider.GOOGLE, "http://socialauth.in/socialauthdemo");
adapter.addCallBack(Provider.SALESFORCE, "https://socialauth.in:8443/socialauthdemo/socialAuthSuccessAction.do");
adapter.addCallBack(Provider.YAMMER, "http://socialauth.in/socialauthdemo/socialAuthSuccessAction.do");
adapter.enable(share);
}
private final class ResponseListener implements DialogListener{
@Override
public void onBack() {
// TODO Auto-generated method stub
Log.d("Share-Button", "Dialog Closed by pressing Back Key");
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
Log.d("ShareButton", "Authentication Cancelled");
}
@Override
public void onComplete(Bundle values) {
// TODO Auto-generated method stub
Log.d("ShareButton", "Authentication Successful");
final String providerName = values.getString(SocialAuthAdapter.PROVIDER);
Log.d("ShareButton", "Provider Name = " + providerName);
Toast.makeText(MainActivity.this, providerName + "connected", Toast.LENGTH_LONG).show();
update = (Button) findViewById(R.id.update);
edit = (EditText) findViewById(R.id.editTxt);
update.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
adapter.updateStatus(edit.getText().toString());
Toast.makeText(MainActivity.this, "Message posted on " + providerName, Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onError(SocialAuthError error) {
// TODO Auto-generated method stub
Log.d("ShareButton", "Authentication Error: " + error.getMessage());
}
}
}
这是我的xml文件
<?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"
android:background="#000000"
>
<LinearLayout
android:orientation="horizontal"
android:id="@+id/topbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="7dp"
android:background="@drawable/blue_gradient"
>
<TextView
android:id="@+id/textview"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="#ffffff"
android:textSize="14sp"
android:text="Share Button"
android:textStyle="bold"
/>
</LinearLayout>
<View
android:id="@+id/view01"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_below="@+id/topbar"
android:background="#313437" />
<TextView
android:id="@+id/text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="14sp"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:textColor="#ffffff"
android:layout_below="@+id/view01"
/>
<TextView
android:id="@+id/dialogTitle"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textColor="#ffffff"
android:textSize="14sp"
android:text="Share Update"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textStyle="bold"
android:layout_below="@+id/text"
/>
<EditText
android:id="@+id/editTxt"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textColor="#000000"
android:textSize="12sp"
android:layout_margin="5dp"
android:hint="Enter Message"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_below="@+id/dialogTitle"
/>
<Button
android:id="@+id/update"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#147af8"
android:textColor="#ffffff"
android:padding="5dp"
android:textSize="12sp"
android:text="Update"
android:layout_alignRight="@+id/editTxt"
android:layout_marginRight="10dp"
android:layout_below="@+id/editTxt"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/layout_gradient"
android:layout_alignParentBottom="true"
>
<Button
android:id="@+id/sharebutton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="20dp"
/>
</RelativeLayout>
</RelativeLayout>
谁能告诉我我的项目出了什么问题?谢谢。
答案 0 :(得分:1)
update = (Button) findViewById(R.id.update);
将此代码移至onCreate()
。希望这可以解决您的问题。
答案 1 :(得分:0)
初始化更新按钮并在onCreate方法中设置onClickListener而不是onComplete