我是 Android 小部件的新手,环顾四周时注意到人们在首次为小部件创建项目时未选中“创建活动”设置,所以我也这样做了。现在,当我运行我的小部件时,我收到了这个错误:
[2013-10-04 18:08:35 - AwesomeFileBuilderWidget] No Launcher activity found!
[2013-10-04 18:08:35 - AwesomeFileBuilderWidget] The launch will only sync the application package on the device!
[2013-10-04 18:08:35 - AwesomeFileBuilderWidget] Performing sync
[2013-10-04 18:08:35 - AwesomeFileBuilderWidget] Automatic Target Mode: Unable to detect device compatibility. Please select a target device.
我现在在想我应该保持选中“创建活动”设置。这会是问题吗?如果是这样,我该如何更改“创建活动”设置?
顺便说一下,这是我的 Android 清单:
<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=".AwesomeFileBuilderWidget" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_stuff"/>
<activity android:name=".WidgetConfig" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</intent-filter>
</activity>
</application>
</manifest>
这是我尝试在真实设备上运行小部件时遇到的完整错误:
[2013-10-04 19:02:33 - AwesomeFileBuilderWidget] ------------------------------
[2013-10-04 19:02:33 - AwesomeFileBuilderWidget] Android Launch!
[2013-10-04 19:02:33 - AwesomeFileBuilderWidget] adb is running normally.
[2013-10-04 19:02:33 - AwesomeFileBuilderWidget] No Launcher activity found!
[2013-10-04 19:02:33 - AwesomeFileBuilderWidget] The launch will only sync the application package on the device!
[2013-10-04 19:02:33 - AwesomeFileBuilderWidget] Performing sync
[2013-10-04 19:02:33 - AwesomeFileBuilderWidget] Automatic Target Mode: Unable to detect device compatibility. Please select a target device.
[2013-10-04 19:02:46 - AwesomeFileBuilderWidget] Uploading AwesomeFileBuilderWidget.apk onto device 'HT18YMA05067'
[2013-10-04 19:02:51 - AwesomeFileBuilderWidget] Failed to install AwesomeFileBuilderWidget.apk on device 'HT18YMA05067': timeout
[2013-10-04 19:02:51 - AwesomeFileBuilderWidget] Launch canceled!
这是我的类和 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);
}
}
小部件.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" >
<requestFocus />
</EditText>
<Button
android:id="@+id/bwidgetconfig"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
注意:这个小部件还没有完全完成,但是我仍然可以将我当前的小部件下载到主屏幕,即使小部件还没有任何功能。