1

我正在创建一个动态壁纸应用程序,其中有一个随机的图片幻灯片以及底部的小缩略图,可以点击。我不得不扩展多个课程,我认为这就是我卡住的地方。请帮忙。

动态壁纸类:

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.View;
import android.widget.AdapterView;

public class LiveWallpaper extends WallpaperService{

public static final String SHARED_PREFS_NAME = "livewallpapersettings";


 @Override
 public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
}


@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
}


@Override
public Engine onCreateEngine() {
    // TODO Auto-generated method stub
    return new TestPatternEngine();
}

class TestPatternEngine extends Engine implements
                           SharedPreferences.OnSharedPreferenceChangeListener 
{
    /*Instance variables.*/
    private AdapterView parent;
    private View v;
    private int position;
    private long id;
    private Bundle savedInstanceState;
    private final Handler mHandler = new Handler();
    private MainActivity        mMainActivity;
    private boolean             mVisible;
    private String              mMode = "random";
    private String              mTransition = "fade";
    private float               mTouchX         = -1;
    private float               mTouchY         = -1;
    private SharedPreferences   mPreferences;
    private final Runnable      mStartApp = new Runnable(){
        public void run() {
            mMainActivity = new MainActivity();
            mMainActivity.onCreate(savedInstanceState);
        }
    };

    TestPatternEngine() {
        mPreferences = LiveWallpaper.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
        mPreferences.registerOnSharedPreferenceChangeListener(this);
        onSharedPreferenceChanged(mPreferences,null);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
        // TODO Auto-generated method stub
        mMode = prefs.getString("livewallpaper_mode", "random");
        mTransition = prefs.getString("livewallpaper_transitions", "fade");


    }

    @Override
    public void onCreate(SurfaceHolder surfaceHolder) {
        // TODO Auto-generated method stub
        super.onCreate(surfaceHolder);
        setTouchEventsEnabled(true);
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        mHandler.removeCallbacks(mStartApp);
    }

    @Override
    public void onVisibilityChanged(boolean visible)
    {
        mVisible = visible;
        if (visible)
        {
            mMainActivity.onCreate(savedInstanceState);
        }
        else
        {
            mHandler.removeCallbacks(mStartApp);
        }
    }

    @Override
    public void onTouchEvent(MotionEvent event)
    {
        if (event.getAction() == MotionEvent.ACTION_MOVE)
        {
            mTouchX = event.getX();
            mTouchY = event.getY();
        }
        else
        {
            mTouchX = -1;
            mTouchY = -1;
        }
        super.onTouchEvent(event);
         }
     }
}

主要活动类:

         import java.util.Random;
         import java.util.Timer;
         import java.util.TimerTask;
         import com.example.LiveWallpaper.LiveWallpaper.TestPatternEngine;
         import android.app.Activity;
         import android.content.Context;
         import android.content.SharedPreferences;
         import android.os.Bundle;
         import android.os.Handler;
         import android.view.View;
         import android.view.ViewGroup;
         import android.view.Window;
         import android.view.animation.AnimationUtils;
         import android.widget.AdapterView;
         import android.widget.BaseAdapter;
         import android.widget.Gallery;
         import android.widget.Gallery.LayoutParams;
         import android.widget.ImageSwitcher;
         import android.widget.ImageView;
         import android.widget.ViewSwitcher;

         public class MainActivity extends Activity implements
                      AdapterView.OnItemSelectedListener,ViewSwitcher.ViewFactory{

     private final Handler mHandler = new Handler();
     private int position =0;


@Override
public void onCreate(Bundle savedInstanceState) {
    int delay = 1000;
    int period = 3000;
    Timer timer = new Timer();
    timer.schedule(new TimerTask(){

        @Override
        public void run() {
            // TODO Auto-generated method stub
            runOnUiThread(new Runnable(){

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                        Random rand = new Random();
                        position = rand.nextInt(mImageIds.length);
                        mSwitcher.setImageResource(mImageIds[position]);
                    }
            });
        }

    }, delay, period);
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);


    setContentView(R.layout.activity_main);

    mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
    mSwitcher.setFactory(this);
    mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
            android.R.anim.fade_in));
    mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
            android.R.anim.fade_out));

    Gallery g = (Gallery) findViewById(R.id.gallery);
    g.setAdapter(new ImageAdapter(this));
    g.setOnItemSelectedListener(this);
}

public void onItemSelected(AdapterView parent, View v, int position, long id) {
    mSwitcher.setImageResource(mImageIds[position]);
}

public void onNothingSelected(AdapterView parent) {
}

public View makeView() {
    ImageView i = new ImageView(this);
    i.setBackgroundColor(0xFF000000);
    i.setScaleType(ImageView.ScaleType.FIT_CENTER);
    i.setLayoutParams(new ImageSwitcher.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    return i;
}

private ImageSwitcher mSwitcher;

public class ImageAdapter extends BaseAdapter {
    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);

        i.setImageResource(mThumbIds[position]);
        i.setAdjustViewBounds(true);
        i.setLayoutParams(new Gallery.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        i.setBackgroundResource(R.drawable.ic_launcher);
        return i;
    }

    private Context mContext;

}

private Integer[] mThumbIds = { R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4, R.drawable.image5 };

private Integer[] mImageIds = { R.drawable.image1,
        R.drawable.image2, R.drawable.image3, R.drawable.image4, R.drawable.image5};

日志猫:

     06-18 13:57:49.534: W/dalvikvm(283): threadid=1: thread exiting with uncaught   exception (group=0x4001d800)
     06-18 13:57:49.584: E/AndroidRuntime(283): FATAL EXCEPTION: main
     06-18 13:57:49.584: E/AndroidRuntime(283): java.lang.NullPointerException
     06-18 13:57:49.584: E/AndroidRuntime(283):     at  com.example.Livewallpaper.LiveWallpaper$TestPatternEngine.onVisibilityChanged(LiveWallpaper.java:96)
     06-18 13:57:49.584: E/AndroidRuntime(283):     at  android.service.wallpaper.WallpaperService$Engine.reportVisibility(WallpaperService.java:637)
     06-18 13:57:49.584: E/AndroidRuntime(283):     at android.service.wallpaper.WallpaperService$Engine.doVisibilityChanged(WallpaperService.java :620)
     06-18 13:57:49.584: E/AndroidRuntime(283):     at android.service.wallpaper.WallpaperService$IWallpaperEngineWrapper.executeMessage(WallpaperService.java:835)
     06-18 13:57:49.584: E/AndroidRuntime(283):     at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:61)
     06-18 13:57:49.584: E/AndroidRuntime(283):     at android.os.Handler.dispatchMessage(Handler.java:99)
     06-18 13:57:49.584: E/AndroidRuntime(283):     at android.os.Looper.loop(Looper.java:123)
     06-18 13:57:49.584: E/AndroidRuntime(283):     at android.app.ActivityThread.main(ActivityThread.java:4627)
     06-18 13:57:49.584: E/AndroidRuntime(283):     at java.lang.reflect.Method.invokeNative(Native Method)
     06-18 13:57:49.584: E/AndroidRuntime(283):     at java.lang.reflect.Method.invoke(Method.java:521)
     06-18 13:57:49.584: E/AndroidRuntime(283):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
     06-18 13:57:49.584: E/AndroidRuntime(283):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
     06-18 13:57:49.584: E/AndroidRuntime(283):     at dalvik.system.NativeStart.main(Native Method)
4

0 回答 0