如何解决:二进制 XML 文件第 8 行:膨胀类时出错 [重复]

Posted

技术标签:

【中文标题】如何解决:二进制 XML 文件第 8 行:膨胀类时出错 [重复]【英文标题】:How to solve : Binary XML file line #8: Error inflating class [duplicate] 【发布时间】:2013-09-22 14:29:59 【问题描述】:

我的应用包括在用户说话时移动图片。我已经这样做了,但是当我尝试将图像设置为我的应用程序的背景时遇到了问题。我正在尝试使用 xml 文件执行此操作,但我得到了 Error inflating 类。任何人都可以帮助我吗? 提前致谢

xml类中的第8行是:

<com.example.prova1.MoveBalloon.DrawBalloon
        android:id="@+id/drawBalloon1"
        android:layout_
        android:layout_
        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 ComponentInfocom.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_
    android:layout_
    tools:context=".MoveBalloon"
        android:background="#00000000" >

    <view class="com.example.prova1.MoveBalloon$DrawBalloon" 

        android:id="@+id/drawBalloon1"
        android:layout_
        android:layout_
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="182dp" 
        />

</RelativeLayout>

【问题讨论】:

【参考方案1】:

你必须使用类属性

看到这个Error referencing an inner class View in layout/main.xml

【讨论】:

我做了这个但是发生了一个错误并说:元素类型“com.example.prova1.MoveBalloon”必须后跟属性规范,“>”或“/>”。 有什么我可以做的吗? 我做了这个,所以我更改了我的 xml 文件,看起来我已经在上面发布了,但我仍然收到此错误:RuntimeException: Unable to start activity ComponentInfocom.example.prova1/com. example.prova1.MoveBalloon: android.view.InflateException: Binary XML file line #8: Error inflating class com.example.prova1.MoveBalloon$DrawBalloon 原因:java.lang.NoSuchMethodException: [class android.content.上下文、接口android.util.AttributeSet] 让你的内部类静态

以上是关于如何解决:二进制 XML 文件第 8 行:膨胀类时出错 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

二进制 XML 文件第 13 行:在扩展 VideoView 的类上膨胀类时出错

如何摆脱问题'二进制XML文件第8行:二进制XML文件第8行:使用XYPlot时会膨胀类com.androidplot.xy.XYPlot类]

android.view.InflateException:二进制 XML 文件第 15 行:二进制 XML 文件第 19 行:膨胀类片段时出错

二进制 XML 文件第 13 行:膨胀类片段时出错

在 Cardview 中膨胀二进制 xml 文件的类时出错

二进制 XML 文件第 2 行:膨胀类 android.widget.RelativeLayout 时出错