I am creating a live wallpaper application for android. I've written some code but I am getting following error Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.service.wallpaper.CHANGE_LIVE_WALLPAPER (has extras) }
Please Help
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>
mywallpaper.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" />
prefs.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>
LOG CAT
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
I don't know what target you're building for but the documentation says that WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER was added in API level 16, so if you're using this code on an older device, it will throw this error. This answer shows how to check for the right API level.
Instead of checking the API level, you can also wrap the intent in a try/catch. While this is bad practice in general, I think it might be beneficial here, as some people have occasionally reported this error on API levels greater than 16 (although this is rare, and may have to do with the user not using a stock android ROM).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With