So I have been trying for hours to get this to work and I can't for the life of me figure it out. I have tried many different ideas I've found just googling it, but without any luck. I am trying to create an android widget that you can click on an image and it uses the default browser to open up a website. I am able to get the image and widget working correctly, but when I try to implement the code for the button to open a website, it kills the widget and says "Problem loading gadget" on the phone. I'm fairly new to making widgets/apps so any advice is appreciated. How can I make the widget work?
Here is my code:
Main.xml
$<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/widget_frame_portrait"
>
<TextView android:id="@+id/xmas"
android:layout_toRightOf="@+id/refresh"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:text="@string/hello"
android:layout_marginTop="37dip"
android:layout_marginBottom="25dip"
android:layout_marginRight="5dip"
android:orientation="vertical"
android:textColor="#000000"/>
<ImageButton
android:layout_height="50dip"
android:id="@+id/refresh"
android:background="@drawable/feep"
android:layout_width="225dip"
android:layout_marginTop="25dip"
android:layout_marginBottom="25dip"
开发者_运维问答 android:layout_marginLeft="25dip"
android:layout_marginRight="1dip"
android:orientation="horizontal"
android:clickable="true"
android:onClick="openWebUrl"
/>
</RelativeLayout>
My only activity called bGiving.Java:
$package chris.days.to;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
public class bGiving extends Activity implements View.OnClickListener {
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
}
public void openWebURL(View v) {
String url = "http://google.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
public void onClick(View v) {
}
}
and my Android Manifest is:
$<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="chris.days.to"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name=".DaysToXmas" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/hello_widget_provider" />
</receiver>
<activity android:name=".bGiving"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
When I add in the code in the main.xml "android:onClick="openWebUrl" it kills the widget.
Set a PendingIntent
to your Button
. This will cause the Intent
to be executed when the Button
is pressed.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(data);
PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
remoteView.setOnClickPendingIntent(R.id.button, pendingIntent);
精彩评论