我使用Eclipse中的Refactor更改了Android项目的包名称,之后我的项目将在Eclipse中的手机上编译和运行。但是,当我将其导出为.apk文件时,它不再起作用并且总是立即崩溃。
这是崩溃时返回的错误:
java.lang.RuntimeException: Unable to instantiate service com.johntcompas.audio_live_wallpaper.AudioWallpaperService: java.lang.NullPointerException
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2556)
at android.app.ActivityThread.access$1800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.johntcompas.audio_live_wallpaper.AudioWallpaperService.<init>(AudioWallpaperService.java:37)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2553)
... 10 more
这是我的java类:
package com.johntcompas.audio_live_wallpaper;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.util.DisplayMetrics;
import android.view.SurfaceHolder;
import android.view.WindowManager;
import android.media.AudioRecord ;
import android.media.AudioFormat ;
import android.media.MediaRecorder.AudioSource;
public class AudioWallpaperService extends WallpaperService
{
public AudioWallpaperService() {
super() ;
}
@Override
public void onCreate()
{
super.onCreate();
colordelemiter = 255/800 ;
exagger = (float) 0.08;
defaultRaduiis = 850 ;
on = pastRadii.size()-1 ;
colorMult = 20;
circleNum = 8;
}
static int bufferSize ;
static AudioRecord recorder ;
static ArrayList<float[]> pastRadii ;
static float cosValues[] ;
static float sinValues[] ;
static ArrayList<Integer> circleEmphasis ;
static float scalarValues[] ;
static float scalarValuesNEXT[] ;
static Paint p ;
static int circleNum ;
static int colorMult ;
static int on ;
static float colordelemiter ;
static boolean firstdraw = true;
static int defaultRaduiis ;
static float exagger ;
static int centerX ;
static int centerY ;
static int width;
static int size ;
static int height;
static float[] radiiMaxValues;
public static void releaseAudio() {
if (recorder != null) {
recorder.stop() ;
recorder.release() ;
recorder = null ;
}
}
public static void captureAudio() {
if (recorder == null) {
bufferSize = AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_IN_MONO,AudioFormat.ENCODING_PCM_16BIT);
recorder = new AudioRecord(AudioSource.MIC, 8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize*10);
recorder.startRecording();
}
}
@Override
public void onDestroy()
{
super.onDestroy();
}
@Override
public Engine onCreateEngine()
{
return new AudioWallpaperEngine();
}
class AudioWallpaperEngine extends Engine
{
private final Handler handler = new Handler();
private final Runnable drawRunner = new Runnable() {
@Override
public void run() {
draw();
}
};
private boolean visible = true;
AudioWallpaperEngine()
{
p = new Paint(Paint.ANTI_ALIAS_FLAG) ;
p.setColor(Color.BLUE);
bufferSize = AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_IN_MONO,AudioFormat.ENCODING_PCM_16BIT);
recorder = new AudioRecord(AudioSource.MIC, 8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize*10);
pastRadii = new ArrayList<float[]>() ;
recorder.startRecording();
radiiMaxValues = new float[bufferSize/4] ;
float deltaAngle = (float) (8*Math.PI/ bufferSize) ;
size = (bufferSize - 1)/4;
circleEmphasis = new ArrayList<Integer> () ;
scalarValues = new float[circleNum] ;
scalarValuesNEXT = new float[circleNum] ;
for (int i = circleNum-1 ; i >= 0 ; i-- ) {
scalarValues[i] = (float)Math.pow(.80,i);
scalarValuesNEXT[i] = scalarValues[i]*.95f ;
}
cosValues = new float[bufferSize/4] ;
sinValues = new float[bufferSize/4] ;
for (int i = 0 ; i < bufferSize/4 ; i++) {
cosValues[i] = (float)Math.cos(deltaAngle*i) ;
sinValues[i] = (float)Math.sin(deltaAngle*i) ;
}
}
public void onCreate(SurfaceHolder surfaceHolder)
{
super.onCreate(surfaceHolder);
height = surfaceHolder.getSurfaceFrame().top/2 ;
width = surfaceHolder.getSurfaceFrame().left/2 ;
}
@Override
public void onVisibilityChanged(boolean visible)
{
this.visible = visible;
if (visible)
{
handler.post(drawRunner);
}
else
{
handler.removeCallbacks(drawRunner);
releaseAudio() ;
}
}
@Override
public void onSurfaceDestroyed(SurfaceHolder holder)
{
super.onSurfaceDestroyed(holder);
this.visible = false;
handler.removeCallbacks(drawRunner);
}
@Override
public void onOffsetsChanged(float xOffset, float yOffset, float xStep, float yStep, int xPixels, int yPixels)
{
draw();
}
short[] buffer ;
void draw()
{
buffer = new short[bufferSize] ;
if (recorder != null) {
bufferSize = recorder.read(buffer,0,buffer.length);
} else {
// need to get recorder
captureAudio() ;
bufferSize = recorder.read(buffer,0,buffer.length);
}
final SurfaceHolder holder = getSurfaceHolder();
Canvas c = null;
try
{
c = holder.lockCanvas();
c.drawColor(Color.BLACK);
if (c != null)
{
float total = 0 ;
float currentRadii[] = new float[size] ;
for(int i = 0; i < size; i++)
{
currentRadii[i] = buffer[i]*exagger+defaultRaduiis ;
total += Math.abs(buffer[i]) ;
}
total /= (size) ;
int currentColor = (int)(colordelemiter*total) ;
pastRadii.add(currentRadii) ;
circleEmphasis.add(currentColor) ;
float scalarRad ;
float scalarRadPlus ;
float rad[]= new float [size];
colorMult = 205 / (on+1) ;
for (int i = on ; i >= 0 ; i-- ) {
rad = pastRadii.get(i) ;
p.setARGB(255, circleEmphasis.get(i), 0, colorMult*i+50) ;
p.setStrokeWidth((float) (scalarValues[i]*10f+1f)) ;
for(int f = 0; f < size-2; f+=2)
{
scalarRad = rad[f]*scalarValues[i];
//if
scalarRadPlus = rad[f+1]*scalarValues[i] ;
c.drawLine(cosValues[f]*scalarRad + width, sinValues[f]*scalarRad + height, cosValues[f+1]*scalarRadPlus + width, sinValues[f+1]*scalarRadPlus + height, p) ;
}
c.drawLine(cosValues[size-2]*rad[size-2]*scalarValues[i] + width, sinValues[size-2]*rad[size-2]*scalarValues[i] + height, cosValues[0]*rad[0]*scalarValues[i] + width, sinValues[0]*rad[0]*scalarValues[i] + height, p) ;
}
if (pastRadii.size() >= circleNum) {
pastRadii.remove(0) ;
circleEmphasis.remove(0) ;
}
}
}
finally
{
if (c != null)
holder.unlockCanvasAndPost(c);
}
handler.removeCallbacks(drawRunner);
if (visible)
{
handler.postDelayed(drawRunner, 10);
}
}
}
}
这是我的.xml文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.johntcompas.audio_live_wallpaper"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="16" />
<uses-feature
android:name="android.software.live_wallpaper"
android:required="true" >
</uses-feature>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/wallpaper_description"
android:theme="@style/AppTheme"
android:permission="android.permission.RECORD_AUDIO">
<service
android:name="com.johntcompas.audio_live_wallpaper.AudioWallpaperService"
android:enabled="true"
android:label="Audio Wallpaper"
android:permission="android.permission.BIND_WALLPAPER"
>
<intent-filter>
<action android:name="android.service.wallpaper.WallpaperService" >
</action>
</intent-filter>
<meta-data
android:name="android.service.wallpaper"
android:resource="@xml/wallpaper" >
</meta-data>
</service>
</application>
</manifest>
任何和所有帮助将不胜感激!