我正在为andriod开发模拟时钟小部件我在小部件中有一个imageview,我需要添加一个点击监听器,所以当它被点击它将启动一个活动(facebook分享活动)这里是代码
public class Widget extends AppWidgetProvider
{
public void onReceive(Context context, Intent intent)
{
super.onReceive(context, intent);
PackageManager packageManager = context.getPackageManager();
Intent alarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
Log.d("ARH","CLICKK");
String clockImpls[][] = {
{"HTC Alarm Clock", "com.htc.android.worldclock", "com.htc.android.worldclock.WorldClockTabControl" },
{"Standar Alarm Clock", "com.android.deskclock", "com.android.deskclock.AlarmClock"},
{"Froyo Nexus Alarm Clock", "com.google.android.deskclock", "com.android.deskclock.DeskClock"},
{"Moto Blur Alarm Clock", "com.motorola.blur.alarmclock", "com.motorola.blur.alarmclock.AlarmClock"},
{"Samsung Galaxy Clock", "com.sec.android.app.clockpackage","com.sec.android.app.clockpackage.ClockPackage"}
};
boolean foundClockImpl = false;
for(int i=0; i<clockImpls.length; i++) {
String vendor = clockImpls[i][0];
String packageName = clockImpls[i][1];
String className = clockImpls[i][2];
try {
ComponentName cn = new ComponentName(packageName, className);
ActivityInfo aInfo = packageManager.getActivityInfo(cn, PackageManager.GET_META_DATA);
alarmClockIntent.setComponent(cn);
foundClockImpl = true;
} catch (Exception e) {
System.out.println(vendor + " does not exists");
}
}
if (foundClockImpl) {
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, alarmClockIntent, 0);
String action = intent.getAction();
Log.v("info",action);
if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action))
{
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent fbook = new Intent(context,FacebookShare.class);
PendingIntent fbookPIntent = PendingIntent.getActivity(context, 0, fbook, 0);
views.setOnClickPendingIntent(R.id.facebooksharebtn, fbookPIntent);
AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views);
}else{
}
}
}
}
和需要出现的活动是
public class FacebookShare extends Activity {
Facebook facebook = new Facebook("304105172993733");
boolean isLoggedIn = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.facebook);
facebook.authorize(this, new String[]{"publish_stream"}, new DialogListener(){
public void onComplete(Bundle values) {
Log.d("COMPLETE","AUTH COMPLETE. VALUES: "+values.size());
Log.d("AUTH TOKEN","== "+values.getString(Facebook.TOKEN));
updateStatus(values.getString(Facebook.TOKEN));
}
public void onFacebookError(FacebookError e) {
Log.d("FACEBOOK ERROR","FB ERROR. MSG: "+e.getMessage()+", CAUSE: "+e.getCause());
}
public void onError(DialogError e) {
Log.e("ERROR","AUTH ERROR. MSG: "+e.getMessage()+", CAUSE: "+e.getCause());
}
public void onCancel() {
Log.d("CANCELLED","AUTH CANCELLED");
}
});
}
public void updateStatus(String accessToken){
try {
Bundle bundle = new Bundle();
bundle.putString("message", "test update");
bundle.putString(Facebook.TOKEN,accessToken);
String response = facebook.request("me/feed",bundle,"POST");
Log.d("UPDATE RESPONSE",""+response);
} catch (MalformedURLException e) {
Log.e("MALFORMED URL",""+e.getMessage());
} catch (IOException e) {
Log.e("IOEX",""+e.getMessage());
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("onActivityResult","onActivityResult");
facebook.authorizeCallback(requestCode, resultCode, data);
}
}
两者都在同一个包中
这是我点击imageview时得到的例外
E / AndroidRuntime(1130):java.lang.RuntimeException:无法启动 活动 ComponentInfo {com.mnet.rockwell.widgets.AnalogClock / com.mnet.widgets.AnalogClock.FacebookShare}: android.view.InflateException:二进制XML文件行#31:错误 膨胀类android.widget.FrameLayout
这也是我没有登录logcat的任何日志
这是我试过的日志
Log.d("ARH","CLICKK");
Log.e("ARH","CLICKK");
Log.v("ARH","CLICKK");
Log.i("ARH","CLICKK");
我是Android开发的新手,但我是Java开发人员,请提出任何建议
这里是facebook.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:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Thank You For Sharing Rockwell Widget" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:layout_marginRight="27dp"
android:layout_marginTop="40dp"
android:text=" Close " />
</RelativeLayout>