找不到活动来处理Intent {act = android.service.wallpaper.CHANGE_LIVE_WALLPAPER(有附加内容)}

时间:2013-02-18 15:40:27

标签: android live-wallpaper activitynotfoundexception

我正在为Android创建一个动态壁纸应用程序。我写了一些代码,但是我收到了以下错误引起:android.content.ActivityNotFoundException:找不到处理Intent的活动{act = android.service.wallpaper.CHANGE_LIVE_WALLPAPER(有附加内容)} < / p>

请帮助

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>

登录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

1 个答案:

答案 0 :(得分:3)

我不知道您正在构建哪个目标,但the documentation表示在API级别16中添加了WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER,因此如果您在旧设备上使用此代码,它将会抛出这个错误。 This answer显示了如何检查正确的API级别。

您还可以将意图包装在try / catch中,而不是检查API级别。虽然这在一般情况下是不好的做法,但我认为这可能是有益的,因为有些人偶尔在API级别大于16时报告此错误(尽管这很少见,并且可能与不使用股票Android ROM的用户有关) )。