我正在创建一个小部件,但是当我运行它时,我在控制台中收到此错误:
[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):