0

我正在创建一个 android 小部件,当我运行我的小部件时,在清理我的工作区并通过应用程序管理器从我的设备中删除该小部件的任何先前实例之后,当我将小部件添加到我的设备时,我收到错误“链接的程序不再安装在您的手机上”。

在这开始发生之前,我曾经收到此错误:

10-06 08:58:29.448: D/AndroidRuntime(6994): Shutting down VM
10-06 08:58:29.448: W/dalvikvm(6994): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
10-06 08:58:29.458: E/AndroidRuntime(6994): FATAL EXCEPTION: main
10-06 08:58:29.458: E/AndroidRuntime(6994): java.lang.RuntimeException: Unable to instantiate receiver com.example.awesomefilebuilderwidget.AFBWidget: java.lang.ClassNotFoundException: com.example.awesomefilebuilderwidget.AFBWidget in loader dalvik.system.PathClassLoader[/data/app/com.example.awesomefilebuilderwidget-1.apk]
10-06 08:58:29.458: E/AndroidRuntime(6994):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2012)
10-06 08:58:29.458: E/AndroidRuntime(6994):     at android.app.ActivityThread.access$2400(ActivityThread.java:135)
10-06 08:58:29.458: E/AndroidRuntime(6994):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1101)
10-06 08:58:29.458: E/AndroidRuntime(6994):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 08:58:29.458: E/AndroidRuntime(6994):     at android.os.Looper.loop(Looper.java:150)
10-06 08:58:29.458: E/AndroidRuntime(6994):     at android.app.ActivityThread.main(ActivityThread.java:4333)
10-06 08:58:29.458: E/AndroidRuntime(6994):     at java.lang.reflect.Method.invokeNative(Native Method)
10-06 08:58:29.458: E/AndroidRuntime(6994):     at java.lang.reflect.Method.invoke(Method.java:507)
10-06 08:58:29.458: E/AndroidRuntime(6994):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-06 08:58:29.458: E/AndroidRuntime(6994):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-06 08:58:29.458: E/AndroidRuntime(6994):     at dalvik.system.NativeStart.main(Native Method)
10-06 08:58:29.458: E/AndroidRuntime(6994): Caused by: java.lang.ClassNotFoundException: com.example.awesomefilebuilderwidget.AFBWidget in loader dalvik.system.PathClassLoader[/data/app/com.example.awesomefilebuilderwidget-1.apk]
10-06 08:58:29.458: E/AndroidRuntime(6994):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
10-06 08:58:29.458: E/AndroidRuntime(6994):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
10-06 08:58:29.458: E/AndroidRuntime(6994):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
10-06 08:58:29.458: E/AndroidRuntime(6994):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2003)
10-06 08:58:29.458: E/AndroidRuntime(6994):     ... 10 more
10-06 08:58:36.535: W/dalvikvm(7066): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)

这是我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.awesomefilebuilderwidget"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<receiver android:name=".AFBWidget" 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/widget_stuff"/>

</receiver>

<activity android:name="com.example.awesomefilebuilder.WidgetConfig"     android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
<category android:name="android.intent.category.LAUNCHER" />     
</intent-filter> 

</activity>   

<activity android:name="com.example.awesomefilebuilderwidget.AFBWidget" android:label="@string/app_name"/>


</application>

</manifest> 

我只有两个类:AFBWidget.java:

import java.util.Random;

import com.example.awesomefilebuilderwidget.R;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.widget.RemoteViews;
import android.widget.Toast;


public class AFBWidget extends AppWidgetProvider{

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    // TODO Auto-generated method stub
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    Random r = new Random();
    int randomInt = r.nextInt(1000000000);
    String rand = String.valueOf(randomInt);

    final int N = appWidgetIds.length;

    for (int i = 0; i < N; i++){
        int awID = appWidgetIds[i];
        RemoteViews v = new RemoteViews(context.getPackageName(), R.layout.widget);
        v.setTextViewText(R.id.tvwidgetUpdate, rand);
        appWidgetManager.updateAppWidget(awID, v);
    }

}

@Override
public void onDeleted(Context context, int[] appWidgetIds) {
    // TODO Auto-generated method stub
    super.onDeleted(context, appWidgetIds);
    Toast.makeText(context, "Thanks for checking us out!", Toast.LENGTH_SHORT).show();
}
}

小部件配置.java:

import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RemoteViews;

import com.example.awesomefilebuilderwidget.R;


public class WidgetConfig extends Activity implements OnClickListener{

EditText info;
AppWidgetManager awm;
Context c;
int awID;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.widgetconfig);
    Button b = (Button)findViewById(R.id.bwidgetconfig);
    b.setOnClickListener(this);
    c = WidgetConfig.this;
    info = (EditText)findViewById(R.id.etwidgetconfig);
    //Getting info about the widget that launched this Activity
    Intent i = getIntent();
    Bundle extras = i.getExtras();
    if (extras != null){
        awID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID
                );
    }else{
        finish();
    }
    awm = AppWidgetManager.getInstance(c);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    String e = info.getText().toString();

    RemoteViews views = new RemoteViews(c.getPackageName(), R.layout.widget);
    views.setTextViewText(R.id.tvConfigInput, e);

    Intent in = new Intent(c, WidgetConfig.class);
    PendingIntent pi = PendingIntent.getActivity(c, 0, in, 0);
    views.setOnClickPendingIntent(R.id.bwidgetOpen, pi);

    awm.updateAppWidget(awID, views);

    Intent result = new Intent();
    result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);
    setResult(RESULT_OK, result);

    finish();
}


}

无论我做什么,我都无法弄清楚如何解决这个错误。怎么了?

4

2 回答 2

1

3 天后我终于修好了!好的,确实是因为我的两个 java 类在默认包中。所以为了解决这个问题,由于我的类是通过不同的包访问的,我创建了一个新的包,它是我的应用程序(com.example.awesomefilebuilderwidget),然后将类移到那里,现在它可以正常工作了!:DD 谢谢你所做的一切,我真的很感激它!

于 2013-10-20T02:16:35.180 回答
1

在您的清单中进行以下修改:

删除这一行:

    <category android:name="android.intent.category.LAUNCHER" />  

您的小部件配置活动不能是启动器活动!小部件配置仅在用户选择您的小部件表单小部件列表后启动,并由 Andorid 操作系统处理。

如果你想要一个启动器活动,它必须是别的东西,你将无法从中添加小部件。小部件只能像我之前描述的那样添加,当用户从小部件列表中单击您的小部件时(您可以选择打开配置)

清除后,还要删除:

    <activity android:name="com.example.awesomefilebuilderwidget.AFBWidget" android:label="@string/app_name"/>

这条线没有意义吗?您在上面几行声明了您的 .AFBWidget 作为接收器,现在您说它肯定不是一个活动。;) 因为它正在扩展 AppWidgetProvider。

此外,在声明您的接收者时,请添加完整的包名,如下所示:

    <receiver android:name="com.example.awesomefilebuilderwidget.AFBWidget" android:label="@string/app_name"> ....

让我知道事情的后续。

于 2013-10-06T17:26:43.127 回答