自定义View编译失败。Binary XML file line #255: Error inflating

前端之家收集整理的这篇文章主要介绍了自定义View编译失败。Binary XML file line #255: Error inflating前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
02-2815:17:16.281:DEBUG/AndroidRuntime(391):ShuttingdownVM
02-2815:17:16.281:WARN/dalvikvm(391):threadid=1:threadexitingwithuncaughtexception(group=0x4001d800)
02-2815:17:16.372:ERROR/AndroidRuntime(391):FATALEXCEPTION:main
02-2815:17:16.372:ERROR/AndroidRuntime(391):java.lang.RuntimeException:UnabletostartactivityComponentInfo{com.min.musicdemo/com.min.musicdemo.MusicDemo}:android.view.InflateException:BinaryXMLfileline#255:Errorinflatingclasscom.min.musicdemo.lyric.LyricView
02-2815:17:16.372:ERROR/AndroidRuntime(391):atandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atandroid.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atandroid.app.ActivityThread.access$2300(ActivityThread.java:125)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atandroid.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atandroid.os.Handler.dispatchMessage(Handler.java:99)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atandroid.os.Looper.loop(Looper.java:123)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atandroid.app.ActivityThread.main(ActivityThread.java:4627)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atjava.lang.reflect.Method.invokeNative(NativeMethod)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atjava.lang.reflect.Method.invoke(Method.java:521)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atdalvik.system.NativeStart.main(NativeMethod)
02-2815:17:16.372:ERROR/AndroidRuntime(391):Causedby:android.view.InflateException:BinaryXMLfileline#255:Errorinflatingclasscom.min.musicdemo.lyric.LyricView
02-2815:17:16.372:ERROR/AndroidRuntime(391):atandroid.view.LayoutInflater.createView(LayoutInflater.java:513)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atandroid.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atandroid.view.LayoutInflater.rInflate(LayoutInflater.java:618)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atandroid.view.LayoutInflater.rInflate(LayoutInflater.java:621)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atandroid.view.LayoutInflater.inflate(LayoutInflater.java:407)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atandroid.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atandroid.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atcom.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atandroid.app.Activity.setContentView(Activity.java:1647)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atcom.min.musicdemo.MusicDemo.onCreate(MusicDemo.java:255)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atandroid.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-2815:17:16.372:ERROR/AndroidRuntime(391):...11more
02-2815:17:16.372:ERROR/AndroidRuntime(391):Causedby:java.lang.reflect.InvocationTargetException
02-2815:17:16.372:ERROR/AndroidRuntime(391):atcom.min.musicdemo.lyric.LyricView.<init>(LyricView.java:39)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atjava.lang.reflect.Constructor.constructNative(NativeMethod)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atjava.lang.reflect.Constructor.newInstance(Constructor.java:446)
02-2815:17:16.372:ERROR/AndroidRuntime(391):atandroid.view.LayoutInflater.createView(LayoutInflater.java:500)
02-2815:17:16.372:ERROR/AndroidRuntime(391):...23more
02-2815:17:16.372:ERROR/AndroidRuntime(391):Causedby:java.lang.NullPointerException
02-2815:17:16.372:ERROR/AndroidRuntime(391):...27more
02-2815:17:16.421:WARN/ActivityManager(60):Forcefinishingactivitycom.min.musicdemo/.MusicDemo
02-2815:17:16.942:WARN/ActivityManager(60):ActivitypausetimeoutforHistoryRecord{44035078com.min.musicdemo/.MusicDemo}
02-2815:17:27.986:WARN/ActivityManager(60):ActivitydestroytimeoutforHistoryRecord{44035078com.min.musicdemo/.MusicDemo}
02-2815:21:17.266:DEBUG/SntpClient(60):requesttimeFailed:java.net.SocketException:Addressfamilynotsupportedbyprotocol

xml中使用
XML/HTML code
?
@H_121_301@
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!--VIEW3*********lyricarea-->
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:id "@+id/llLyric"
android:orientation "vertical"
android:layout_width "fill_parent"
android:layout_height "fill_parent"
android:visibility "gone"
android:layout_weight "1"
>
TextView
"@+id/tvLyricTitle"
"fill_parent"
"wrap_content"
android:paddingLeft "4dip"
android:text "动感歌词"
android:textColor "#0000FF"
android:layout_gravity "center_horizontal"
android:gravity "center"
/>
com.min.musicdemo.lyric.LyricView
"@+id/lyricView"
"fill_parent"
"200dip"
/>
</ LinearLayout >

类定义
Java code
?
25
26
27
28
29
30
31
32
33
34
@H_121_301@
public class LyricView extends View{
private static final StringTAG= "LyricView" ;
private Paintpaint;
Rectrect; //歌词显示区域
List<Sentence>list= null ; //歌词list
Stringtext= "Lyricisloading..." //单行歌词
long currentTime= 0 //正在演唱的时间
int color= 0xFF0000FF //歌词颜色
public LyricView(Contextcontext){
super (context);
//生成paint
paint= new Paint();
rect.left= this .getLeft();
rect.top= .getTop();
rect.right= .getRight();
rect.bottom= .getBottom();
}
LyricView(Contextcontext,AttributeSetattrs){ //构造方法;根据需要实现继承自View的方法
(context,attrs);
//生成paint
Paint();
rect.left= 10 ;
rect.top= 50 ;
rect.right= ;
rect.bottom= ;
//对于我们自定义的类中,我们需要使用一个名为obtainStyledAttributes的方法获取我们的定义。
//得到自定义控件的属性值。
}

靠,原来是在构造函数中忘了创建rect对象
加上rect=newRect();就ok了

猜你在找的XML相关文章