我的应用程序包括在用户说话时移动图片。我已经这样做了,但是当我尝试将图像设置为我的应用程序的背景时遇到了问题。我正在尝试使用 xml 文件执行此操作,但我得到了 Error inflating 类。任何人都可以帮助我吗?提前致谢
xml 类中的第 8 行是:
<com.example.prova1.MoveBalloon.DrawBalloon
android:id="@+id/drawBalloon1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="182dp" />
logcat 说:
09-22 14:20:56.391: E/AndroidRuntime(20776): FATAL EXCEPTION: main
09-22 14:20:56.391: E/AndroidRuntime(20776): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prova1/com.example.prova1.MoveBalloon}: android.view.InflateException: Binary XML file line #8: Error inflating class com.example.prova1.MoveBalloon.DrawBalloon
09-22 14:20:56.391: E/AndroidRuntime(20776): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
09-22 14:20:56.391: E/AndroidRuntime(20776): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
09-22 14:20:56.391: E/AndroidRuntime(20776): at android.app.ActivityThread.access$700(ActivityThread.java:143)
09-22 14:20:56.391: E/AndroidRuntime(20776): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
09-22 14:20:56.391: E/AndroidRuntime(20776): at android.os.Handler.dispatchMessage(Handler.java:99)
09-22 14:20:56.391: E/AndroidRuntime(20776): at android.os.Looper.loop(Looper.java:137)
09-22 14:20:56.391: E/AndroidRuntime(20776): at android.app.ActivityThread.main(ActivityThread.java:4963)
09-22 14:20:56.391: E/AndroidRuntime(20776): at java.lang.reflect.Method.invokeNative(Native Method)
09-22 14:20:56.391: E/AndroidRuntime(20776): at java.lang.reflect.Method.invoke(Method.java:511)
09-22 14:20:56.391: E/AndroidRuntime(20776): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
09-22 14:20:56.391: E/AndroidRuntime(20776): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
09-22 14:20:56.391: E/AndroidRuntime(20776): at dalvik.system.NativeStart.main(Native Method)
09-22 14:20:56.391: E/AndroidRuntime(20776): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.example.prova1.MoveBalloon.DrawBalloon
09-22 14:20:56.391: E/AndroidRuntime(20776): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
09-22 14:20:56.391: E/AndroidRuntime(20776): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
09-22 14:20:56.391: E/AndroidRuntime(20776): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
09-22 14:20:56.391: E/AndroidRuntime(20776): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
09-22 14:20:56.391: E/AndroidRuntime(20776): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
09-22 14:20:56.391: E/AndroidRuntime(20776): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:318)
09-22 14:20:56.391: E/AndroidRuntime(20776): at android.app.Activity.setContentView(Activity.java:1924)
09-22 14:20:56.391: E/AndroidRuntime(20776): at com.example.prova1.MoveBalloon.onCreate(MoveBalloon.java:95)
09-22 14:20:56.391: E/AndroidRuntime(20776): at android.app.Activity.performCreate(Activity.java:5184)
09-22 14:20:56.391: E/AndroidRuntime(20776): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
09-22 14:20:56.391: E/AndroidRuntime(20776): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
09-22 14:20:56.391: E/AndroidRuntime(20776): ... 11 more
09-22 14:20:56.391: E/AndroidRuntime(20776): Caused by: java.lang.ClassNotFoundException: com.example.prova1.MoveBalloon.DrawBalloon
09-22 14:20:56.391: E/AndroidRuntime(20776): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
09-22 14:20:56.391: E/AndroidRuntime(20776): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
09-22 14:20:56.391: E/AndroidRuntime(20776): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
09-22 14:20:56.391: E/AndroidRuntime(20776): at android.view.LayoutInflater.createView(LayoutInflater.java:558)
09-22 14:20:56.391: E/AndroidRuntime(20776): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:693)
09-22 14:20:56.391: E/AndroidRuntime(20776): ... 21 more
我试图与 xml 连接的 MoveBallon 类是这个
package com.example.prova1;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MoveBalloon extends Activity {
Bitmap balloon;
DrawBalloon myView;
float x,y,sensorX, sensorY;
SensorManager sm;
Microphone mic;
public class DrawBalloon extends SurfaceView implements Runnable {
SurfaceHolder ourHolder ;
Thread ourThread = null;
boolean isRunning=true;
public DrawBalloon(Context context) {
super(context);
ourHolder= getHolder();
}
public DrawBalloon(Context context, AttributeSet attrs) {
super(context, attrs);
ourHolder= getHolder();
}
public void pause() {
isRunning=false;
while(true){
try{
ourThread.join();
} catch (InterruptedException e){
e.printStackTrace();
}
break;
}
ourThread=null;
}
public void resume(){
isRunning=true;
ourThread = new Thread(this);
ourThread.start();
}
@Override
public void run (){
while(isRunning){
if(!ourHolder.getSurface().isValid())
continue;
Canvas canvas = ourHolder.lockCanvas();
updateMic();
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(balloon, sensorX, sensorY,null);
ourHolder.unlockCanvasAndPost(canvas);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
/*sm= (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if(sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size()!=0){
Sensor s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(o);
sm.registerListener(this,s ,SensorManager.SENSOR_DELAY_NORMAL);
}*/
mic = new Microphone();
balloon = BitmapFactory.decodeResource(getResources(), R.drawable.images);
sensorX=150;
sensorY=350;
//x=y=sensorX=sensorY=0;
//myView= new DrawBalloon (this);
//myView.resume();
//setContentView(R.layout.move_balloon);
setContentView(R.layout.move_balloon);
}
/*@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sensorX=event.values[0];
sensorY=event.values[1];
}
*/
public void updateMic(){
int level = mic.getLevel();
sensorY-=level;
}
@Override
public void onBackPressed() {
finish();
}
}
这是我的新 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=".MoveBalloon"
android:background="#00000000" >
<view class="com.example.prova1.MoveBalloon$DrawBalloon"
android:id="@+id/drawBalloon1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="182dp"
/>
</RelativeLayout>