按钮创建时窗口小部件崩溃

时间:2014-03-24 20:08:38

标签: android button widget

第一:我是纽比。因此,如果这是一个令人惊讶的简单解决方案,请不要骚扰我。

我是小部件的新手,我想开始做它们。实际上是一个手电筒程序。

问题是:我可以创建一个Widget,但是当我甚至只是在布局文件中声明一个按钮时,小部件崩溃了,我唯一能看到的就是“Fehler”(在德语中意为“错误”)。

这是XML文件:

<?xml version="1.0" encoding="utf-8"?>
    <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
        android:minHeight="40dp"
        android:minWidth="40dp"
        android:resizeMode=""
        android:updatePeriodMillis="100"
        android:initialLayout="@layout/widget_switch"
        android:widgetCategory="home_screen">

    </appwidget-provider>

布局文件是:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" 
        android:background="@drawable/appwidget_dark_bg">

        <button
        android:id="@+id/switchButton"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textOn="@string/toggle"/>

    </LinearLayout>

Manifest的接收器部分是:

<receiver

    android:label="Flashlight Widget"
    android:name="MyWidgetProvider" >
    <intent-filter >
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        <action android:name="android.appwidget.action.WIDGET_BUTTON" />
    </intent-filter>

    <meta-data                
    android:name="android.appwidget.provider"
    android:resource="@xml/widget_switch" />

</receiver> 

和java文件一样,它与MainActivity在同一个包中

package com.quicksilver.flashlight;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.widget.RemoteViews;

public class widget_switch extends AppWidgetProvider {

    boolean clicked = true;
    boolean camera_initialized=false;
    Camera mCamera;
    public static String WIDGET_BUTTON = "android.appwidget.action.WIDGET_BUTTON";

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[]     appWidgetIds) {

        if (!camera_initialized){
                mCamera=Camera.open();
                camera_initialized=true;
        }
        RemoteViews remoteViews = null;
        remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_switch);

        Intent intent = new Intent(WIDGET_BUTTON);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.switchButton, pendingIntent );
    }



    public void onRecieve(Context context, Intent intent) {
     if (WIDGET_BUTTON.equals(intent.getAction()))
        toggleFlashlight();

    }

    private void turnOn() {

        //mCamera = Camera.open();
        Parameters params = mCamera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_TORCH); 
        mCamera.setParameters(params);
        mCamera.startPreview();

    }

    private void turnOff() { 

        //mCamera = Camera.open();
        Parameters params = mCamera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_OFF); 
        mCamera.setParameters(params);
        mCamera.startPreview();
        //mCamera = Camera.open();
    }

    public void toggleFlashlight () {
        if (clicked){
            clicked = false;
            turnOn();
        }else {
            clicked = true;
            turnOff();

    }
}

logcat的:

03-24 20:40:07.583: D/AndroidRuntime(11560): Shutting down VM
03-24 20:40:07.583: W/dalvikvm(11560): threadid=1: thread exiting with uncaught exception (group=0x41635d40)
03-24 20:40:07.586: E/AndroidRuntime(11560): FATAL EXCEPTION: main
03-24 20:40:07.586: E/AndroidRuntime(11560): Process: com.quicksilver.flashlight, PID: 11560
03-24 20:40:07.586: E/AndroidRuntime(11560): java.lang.RuntimeException: Unable to instantiate receiver com.quicksilver.flashlight.MyWidgetProvider: java.lang.ClassNotFoundException: Didn't find class "com.quicksilver.flashlight.MyWidgetProvider" on path: DexPathList[[zip file "/data/app/com.quicksilver.flashlight-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.quicksilver.flashlight-1, /vendor/lib, /system/lib]]
03-24 20:40:07.586: E/AndroidRuntime(11560):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2427)
03-24 20:40:07.586: E/AndroidRuntime(11560):    at android.app.ActivityThread.access$1700(ActivityThread.java:139)
03-24 20:40:07.586: E/AndroidRuntime(11560):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
03-24 20:40:07.586: E/AndroidRuntime(11560):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-24 20:40:07.586: E/AndroidRuntime(11560):    at android.os.Looper.loop(Looper.java:136)
03-24 20:40:07.586: E/AndroidRuntime(11560):    at android.app.ActivityThread.main(ActivityThread.java:5102)
03-24 20:40:07.586: E/AndroidRuntime(11560):    at java.lang.reflect.Method.invokeNative(Native Method)
03-24 20:40:07.586: E/AndroidRuntime(11560):    at java.lang.reflect.Method.invoke(Method.java:515)
03-24 20:40:07.586: E/AndroidRuntime(11560):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
03-24 20:40:07.586: E/AndroidRuntime(11560):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
03-24 20:40:07.586: E/AndroidRuntime(11560):    at dalvik.system.NativeStart.main(Native Method)
03-24 20:40:07.586: E/AndroidRuntime(11560): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.quicksilver.flashlight.MyWidgetProvider" on path: DexPathList[[zip file "/data/app/com.quicksilver.flashlight-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.quicksilver.flashlight-1, /vendor/lib, /system/lib]]
03-24 20:40:07.586: E/AndroidRuntime(11560):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
03-24 20:40:07.586: E/AndroidRuntime(11560):    at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
03-24 20:40:07.586: E/AndroidRuntime(11560):    at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
03-24 20:40:07.586: E/AndroidRuntime(11560):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2422)
03-24 20:40:07.586: E/AndroidRuntime(11560):    ... 10 more
03-24 20:40:09.361: I/Process(11560): Sending signal. PID: 11560 SIG: 9

因此,您可以使用大量材料。手机是摩托罗拉Moto G,我不在模拟器上测试,因为它太慢了。

我希望你能找到问题并解决它,因为我有点沮丧。

感谢回答!

2 个答案:

答案 0 :(得分:0)

您应该更改以下内容:

android:name="MyWidgetProvider"

android:name="widget_switch"
清单中的

根据receiver tag documentation,名称必须与套餐中的班级名称相匹配。

答案 1 :(得分:0)

(实际上删除了原来的帖子。)

如果您重新制作文件并选择按钮作为根元素,则可以正常工作。

按下它时没有响应,但这属于另一个问题。