我正在为 android 创建一个动态壁纸应用程序。我已经写了一些代码,但我收到以下错误原因:android.content.ActivityNotFoundException:没有找到处理 Intent { act=android.service.wallpaper.CHANGE_LIVE_WALLPAPER (has extras) }
请帮忙
MyPoint.java
public class MyPoint {
public String text;
public float x;
public float y;
public MyPoint(String text, float x, float y) {
this.text = text;
this.x = x;
this.y = y;
}
}
MyPreferencesActivity.java
public class MyPreferencesActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
Preference circlePreference = getPreferenceScreen().findPreference(
"numberOfCircles");
circlePreference.setOnPreferenceChangeListener(numberCheckListener);
}
Preference.OnPreferenceChangeListener numberCheckListener = new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (newValue != null && newValue.toString().length() > 0
&& newValue.toString().matches("\\d*")) {
return true;
}
Toast.makeText(MyPreferencesActivity.this, "Invalid Input",
Toast.LENGTH_SHORT).show();
return false;
}
};
}
MyWallpaperService.java
public class MyWallpaperService extends WallpaperService {
@Override
public Engine onCreateEngine() {
return new MyWallpaperEngine();
}
private class MyWallpaperEngine extends Engine {
private final Handler handler = new Handler();
private final Runnable drawRunner = new Runnable() {
@Override
public void run() {
draw();
}
};
private List<MyPoint> circles;
private Paint paint = new Paint();
private int width;
int height;
private boolean visible = true;
private int maxNumber;
private boolean touchEnabled;
public MyWallpaperEngine() {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(MyWallpaperService.this);
maxNumber = Integer
.valueOf(prefs.getString("numberOfCircles", "4"));
touchEnabled = prefs.getBoolean("touch", false);
circles = new ArrayList<MyPoint>();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(10f);
handler.post(drawRunner);
}
@Override
public void onVisibilityChanged(boolean visible) {
this.visible = visible;
if (visible) {
handler.post(drawRunner);
} else {
handler.removeCallbacks(drawRunner);
}
}
@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
this.visible = false;
handler.removeCallbacks(drawRunner);
}
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
this.width = width;
this.height = height;
super.onSurfaceChanged(holder, format, width, height);
}
@Override
public void onTouchEvent(MotionEvent event) {
if (touchEnabled) {
float x = event.getX();
float y = event.getY();
SurfaceHolder holder = getSurfaceHolder();
Canvas canvas = null;
try {
canvas = holder.lockCanvas();
if (canvas != null) {
canvas.drawColor(Color.BLACK);
circles.clear();
circles.add(new MyPoint(
String.valueOf(circles.size() + 1), x, y));
drawCircles(canvas, circles);
}
} finally {
if (canvas != null)
holder.unlockCanvasAndPost(canvas);
}
super.onTouchEvent(event);
}
}
private void draw() {
SurfaceHolder holder = getSurfaceHolder();
Canvas canvas = null;
try {
canvas = holder.lockCanvas();
if (canvas != null) {
if (circles.size() >= maxNumber) {
circles.clear();
}
int x = (int) (width * Math.random());
int y = (int) (height * Math.random());
circles.add(new MyPoint(String.valueOf(circles.size() + 1),
x, y));
drawCircles(canvas, circles);
}
} finally {
if (canvas != null)
holder.unlockCanvasAndPost(canvas);
}
handler.removeCallbacks(drawRunner);
if (visible) {
handler.postDelayed(drawRunner, 5000);
}
}
// Surface view requires that all elements are drawn completely
private void drawCircles(Canvas canvas, List<MyPoint> circles) {
canvas.drawColor(Color.BLACK);
for (MyPoint point : circles) {
canvas.drawCircle(point.x, point.y, 20.0f, paint);
}
}
}
}
SetWallpaperActivity.java
public class SetWallpaperActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_wallpaper);
}
public void clickMe(View view) {
Intent intent = new Intent(
WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
new ComponentName(this, MyWallpaperService.class));
startActivity(intent);
}
}
activity_set_wallpaper.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SetWallpaperActivity" >
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:onClick="clickMe"
android:text="Click Me!" />
</RelativeLayout>
我的壁纸.xml
<?xml version="1.0" encoding="utf-8"?>
<wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/wallpaper_description"
android:settingsActivity="de.vogella.android.wallpaper.MyPreferencesActivity"
android:thumbnail="@drawable/icon" />
首选项.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:key="touch"
android:title="Enable Touch" >
</CheckBoxPreference>
<EditTextPreference
android:key="numberOfCircles"
android:title="Number of Circles" >
</EditTextPreference>
</PreferenceScreen>
日志猫
02-18 21:08:07.630: E/AndroidRuntime(9406): FATAL EXCEPTION: main
02-18 21:08:07.630: E/AndroidRuntime(9406): java.lang.IllegalStateException: Could not execute method of the activity
02-18 21:08:07.630: E/AndroidRuntime(9406): at android.view.View$1.onClick(View.java:3095)
02-18 21:08:07.630: E/AndroidRuntime(9406): at android.view.View.performClick(View.java:3567)
02-18 21:08:07.630: E/AndroidRuntime(9406): at android.view.View$PerformClick.run(View.java:14224)
02-18 21:08:07.630: E/AndroidRuntime(9406): at android.os.Handler.handleCallback(Handler.java:605)
02-18 21:08:07.630: E/AndroidRuntime(9406): at android.os.Handler.dispatchMessage(Handler.java:92)
02-18 21:08:07.630: E/AndroidRuntime(9406): at android.os.Looper.loop(Looper.java:137)
02-18 21:08:07.630: E/AndroidRuntime(9406): at android.app.ActivityThread.main(ActivityThread.java:4517)
02-18 21:08:07.630: E/AndroidRuntime(9406): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 21:08:07.630: E/AndroidRuntime(9406): at java.lang.reflect.Method.invoke(Method.java:511)
02-18 21:08:07.630: E/AndroidRuntime(9406): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
02-18 21:08:07.630: E/AndroidRuntime(9406): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
02-18 21:08:07.630: E/AndroidRuntime(9406): at dalvik.system.NativeStart.main(Native Method)
02-18 21:08:07.630: E/AndroidRuntime(9406): Caused by: java.lang.reflect.InvocationTargetException
02-18 21:08:07.630: E/AndroidRuntime(9406): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 21:08:07.630: E/AndroidRuntime(9406): at java.lang.reflect.Method.invoke(Method.java:511)
02-18 21:08:07.630: E/AndroidRuntime(9406): at android.view.View$1.onClick(View.java:3090)
02-18 21:08:07.630: E/AndroidRuntime(9406): ... 11 more
02-18 21:08:07.630: E/AndroidRuntime(9406): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.service.wallpaper.CHANGE_LIVE_WALLPAPER (has extras) }
02-18 21:08:07.630: E/AndroidRuntime(9406): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1536)
02-18 21:08:07.630: E/AndroidRuntime(9406): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1388)
02-18 21:08:07.630: E/AndroidRuntime(9406): at android.app.Activity.startActivityForResult(Activity.java:3195)
02-18 21:08:07.630: E/AndroidRuntime(9406): at android.app.Activity.startActivity(Activity.java:3302)
02-18 21:08:07.630: E/AndroidRuntime(9406): at de.vogella.android.wallpaper.SetWallpaperActivity.clickMe(SetWallpaperActivity.java:23)
02-18 21:08:07.630: E/AndroidRuntime(9406): ... 14 more