Android AppWidget: Error inflating Class
<?xml version="1.0" encoding="utf-8"?>
<com.org.BatteryManager.BatteryView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
package com.org.BatteryManager;
import java.util.Map;
import android.view.View;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.os.BatteryManager;
public class BatteryView extends View{
public BatteryView(Context context){
super(context);
init();
}
public BatteryView(Context context, AttributeSet attrs){
super(context,attrs);
init();
}
public BatteryView(Context context, AttributeSet attrs, int defaultStyles){
super(context, attrs, defaultStyles);
init();
}
public void init(){
}
protected void onMeasure(int widthSpec, int heightSpec){
int measuredWidth = M开发者_StackOverflow中文版easureSpec.getSize(widthSpec);
int measuredHeight = MeasureSpec.getSize(heightSpec);
setMeasuredDimension( measuredWidth, measuredHeight);
}
@Override
protected void onDraw(Canvas canvas){
Paint mTextPaint =new Paint();
mTextPaint.setColor(Color.GREEN);
mTextPaint.setTextSize(40);
int height = getMeasuredHeight();
int width = getMeasuredWidth();
String displayText = "Widget";
float textWidth = mTextPaint.measureText(displayText);
int px= width/2;
int py=height/2;
canvas.drawText(displayText, px-textWidth/2, py, mTextPaint);
invalidate();
}
}
My constructors are perfect but I am not sure about that xml.
I think you need to specify the type of Layout you're using. So for example you should have:
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</AbsoluteLayout>
This would be for an absolute layout type.
精彩评论