2

我正在创建一个小部件,但是当我运行它时,我在控制台中收到此错误:

[2013-10-05 22:02:56 - AwesomeFileBuilderWidget] ------------------------------
[2013-10-05 22:02:56 - AwesomeFileBuilderWidget] Android Launch!
[2013-10-05 22:02:56 - AwesomeFileBuilderWidget] adb is running normally.
[2013-10-05 22:02:56 - AwesomeFileBuilderWidget] No Launcher activity found!
[2013-10-05 22:02:56 - AwesomeFileBuilderWidget] The launch will only sync the application package on the device!
[2013-10-05 22:02:56 - AwesomeFileBuilderWidget] Performing sync
[2013-10-05 22:02:56 - AwesomeFileBuilderWidget] Automatic Target Mode: Unable to detect device compatibility. Please select a target device.
[2013-10-05 22:03:00 - AwesomeFileBuilderWidget] Uploading AwesomeFileBuilderWidget.apk onto device 'HT18YMA05067'
[2013-10-05 22:03:00 - AwesomeFileBuilderWidget] Installing AwesomeFileBuilderWidget.apk...
[2013-10-05 22:03:04 - AwesomeFileBuilderWidget] Success!
[2013-10-05 22:03:04 - AwesomeFileBuilderWidget] \AwesomeFileBuilderWidget\bin\AwesomeFileBuilderWidget.apk installed on device
[2013-10-05 22:03:04 - AwesomeFileBuilderWidget] Done!

但小部件在我的设备上安装良好,并显示在小部件选择屏幕中。

当我尝试将小部件放在主屏幕上时,会发生第二个错误。我在 LogCat 中收到此错误:

10-05 21:51:45.485: D/AndroidRuntime(3557): Shutting down VM
10-05 21:51:45.485: W/dalvikvm(3557): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
10-05 21:51:45.505: E/AndroidRuntime(3557): FATAL EXCEPTION: main
10-05 21:51:45.505: E/AndroidRuntime(3557): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.awesomefilebuilderwidget/com.example.awesomefilebuilderwidget.WidgetConfig}: java.lang.ClassNotFoundException: com.example.awesomefilebuilderwidget.WidgetConfig in loader dalvik.system.PathClassLoader[/data/app/com.example.awesomefilebuilderwidget-1.apk]
10-05 21:51:45.505: E/AndroidRuntime(3557):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1746)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:1854)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at android.app.ActivityThread.access$1500(ActivityThread.java:135)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1041)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at android.os.Looper.loop(Looper.java:150)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at android.app.ActivityThread.main(ActivityThread.java:4333)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at java.lang.reflect.Method.invokeNative(Native Method)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at java.lang.reflect.Method.invoke(Method.java:507)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at dalvik.system.NativeStart.main(Native Method)
10-05 21:51:45.505: E/AndroidRuntime(3557): Caused by: java.lang.ClassNotFoundException: com.example.awesomefilebuilderwidget.WidgetConfig in loader dalvik.system.PathClassLoader[/data/app/com.example.awesomefilebuilderwidget-1.apk]
10-05 21:51:45.505: E/AndroidRuntime(3557):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at android.app.Instrumentation.newActivity(Instrumentation.java:1040)
10-05 21:51:45.505: E/AndroidRuntime(3557):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1738)
10-05 21:51:45.505: E/AndroidRuntime(3557):     ... 11 more

这是我的清单:

<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=".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=".AFBWidget" android:label="@string/app_name"/>


</application>

</manifest>

我已确保已选中“订购和导出”部分中的所有框。我还确保我的 AFBWidget 和 WidgetConfig 类在 src 中。(请注意,实际的小部件配置位于正确的 xml 位置,WidgetConfig.java 是别的)

问题是什么?

更新的 LogCat:

10-05 22:47:40.628: D/AndroidRuntime(4113): Shutting down VM
10-05 22:47:40.628: W/dalvikvm(4113): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
10-05 22:47:40.638: E/AndroidRuntime(4113): FATAL EXCEPTION: main
10-05 22:47:40.638: E/AndroidRuntime(4113): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.awesomefilebuilderwidget/com.example.awesomefilebuilderwidget.WidgetConfig}: java.lang.ClassNotFoundException: com.example.awesomefilebuilderwidget.WidgetConfig in loader dalvik.system.PathClassLoader[/data/app/com.example.awesomefilebuilderwidget-1.apk]
10-05 22:47:40.638: E/AndroidRuntime(4113):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1746)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1854)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at android.app.ActivityThread.access$1500(ActivityThread.java:135)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1041)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at android.os.Looper.loop(Looper.java:150)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at android.app.ActivityThread.main(ActivityThread.java:4333)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at java.lang.reflect.Method.invokeNative(Native Method)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at java.lang.reflect.Method.invoke(Method.java:507)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at dalvik.system.NativeStart.main(Native Method)
10-05 22:47:40.638: E/AndroidRuntime(4113): Caused by: java.lang.ClassNotFoundException: com.example.awesomefilebuilderwidget.WidgetConfig in loader dalvik.system.PathClassLoader[/data/app/com.example.awesomefilebuilderwidget-1.apk]
10-05 22:47:40.638: E/AndroidRuntime(4113):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at android.app.Instrumentation.newActivity(Instrumentation.java:1040)
10-05 22:47:40.638: E/AndroidRuntime(4113):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1738)
10-05 22:47:40.638: E/AndroidRuntime(4113):     ... 11 more

添加的类和 XML 文件: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);
    }

}

小部件配置.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();
}


}

小部件.xml:

<?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="vertical" >

<TextView
    android:id="@+id/tvConfigInput"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<Button
    android:id="@+id/bwidgetOpen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

<TextView
    android:id="@+id/tvwidgetUpdate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

</LinearLayout>

小部件配置.xml:

<?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="vertical" >

<EditText
    android:id="@+id/etwidgetconfig"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" 
    android:hint="@string/user_input_application_name">

    <requestFocus />
</EditText>

<Button
    android:id="@+id/bwidgetconfig"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/ok" />

</LinearLayout>

应用小部件提供程序 (app_stuff.xml): 在此处输入图像描述

4

3 回答 3

2

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

于 2013-10-07T21:17:29.423 回答
1

尝试清理您的工作区。转到项目->清理并选择您的项目。由于某种原因,eclipse 不会自动构建清单。

并尝试通过右键单击然后清理来清理服务器

于 2013-10-06T04:23:41.990 回答
1

将 src 中的所有活动添加到 AndroidManifest.xml.clean 并运行项目然后

于 2013-10-06T04:26:06.903 回答