另一天,另一个错误啊,新程序员的生活啊。我正在尝试将bitmap
绘制成TextView
或ImageView
,但它无效。我一直在看这段代码几个小时,试图找出它为什么不起作用。下面是代码。我没有收到任何错误,程序运行,它只是不显示位图
CustomView.java
public class CustomView extends ImageView {
public CustomView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
@Override
public void onDraw(Canvas canvas) {
Bitmap line = BitmapFactory.decodeResource(getResources(), R.drawable.line);
canvas.drawBitmap(line, 0, 0, null);
super.onDraw(canvas);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Hail Mary. If this works then look up how to really use 'onMeasure()'.
setMeasuredDimension(200, 50);
}
主要活动
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
refreshView();
}
private void refreshView() {
Spinner spinner = (Spinner) findViewById(R.id.location_spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, model.getLocationsArray());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
SimpleDateFormat sdf = new SimpleDateFormat("E, MMM dd");
TextView goalText = (TextView) findViewById(R.id.goal_Id);
TextView dateText = (TextView) findViewById(R.id.date_Id);
TextView countText = (TextView) findViewById(R.id.count);
TextView daysText = (TextView) findViewById(R.id.days);
TextView totalText = (TextView) findViewById(R.id.total);
TextView aveText = (TextView) findViewById(R.id.ave);
GregorianCalendar now = new GregorianCalendar();
goalText.setTextColor(Color.RED);
goalText.setText("Today's Goal: Don't Smoke at ");
dateText.setText(sdf.format(now.getTime()));
//get today's count from data in the SQLite table - count entries with today's date
countText.setText("" + mySmokinDatabase.getTodaysCount());
// Table data
daysText.setText("" + String.format("%10d", model.getDays()));
totalText.setText("" + mySmokinDatabase.getTotal());
if (model.getDays() > 0)
aveText.setText("" + mySmokinDatabase.getTotal()/model.getDays());
}
MainActivity.xml
<ImageView class="com.example.smokin4thomassullivan.CustomView"
android:id="@+id/line_Id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/line"
android:textAppearance="?android:attr/textAppearanceSmall" />
logcat的
03-22 20:17:47.272: E/AndroidRuntime(1474): FATAL EXCEPTION: main
03-22 20:17:47.272: E/AndroidRuntime(1474): java.lang.RuntimeException: Unable to start
activity ComponentInfo{com.example.smokin4ThomasSullivan/com.example.smokin4
ThomasSullivan.MainActivity}: android.view.InflateException: Binary XML file line #21:
Error inflating class com.example.smokin4thomassullivan.CustomView
03-22 20:17:47.272: E/AndroidRuntime(1474): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
android.app.ActivityThread.access$600(ActivityThread.java:130)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
android.os.Handler.dispatchMessage(Handler.java:99)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
android.os.Looper.loop(Looper.java:137)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
android.app.ActivityThread.main(ActivityThread.java:4745)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
java.lang.reflect.Method.invokeNative(Native Method)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
java.lang.reflect.Method.invoke(Method.java:511)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
dalvik.system.NativeStart.main(Native Method)
03-22 20:17:47.272: E/AndroidRuntime(1474): Caused by: android.view.InflateException:
Binary XML file line #21: Error inflating class
com.example.smokin4thomassullivan.CustomView
03-22 20:17:47.272: E/AndroidRuntime(1474): at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
android.app.Activity.setContentView(Activity.java:1867)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
com.example.smokin4ThomasSullivan.MainActivity.onCreate(MainActivity.java:40)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
android.app.Activity.performCreate(Activity.java:5008)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-22 20:17:47.272: E/AndroidRuntime(1474): ... 11 more
03-22 20:17:47.272: E/AndroidRuntime(1474): Caused by:
java.lang.ClassNotFoundException: com.example.smokin4thomassullivan.CustomView
03-22 20:17:47.272: E/AndroidRuntime(1474): at
dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
java.lang.ClassLoader.loadClass(ClassLoader.java:501)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
java.lang.ClassLoader.loadClass(ClassLoader.java:461)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
android.view.LayoutInflater.createView(LayoutInflater.java:552)
03-22 20:17:47.272: E/AndroidRuntime(1474): at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
03-22 20:17:47.272: E/AndroidRuntime(1474): ... 21 more`
感谢您的帮助!
答案 0 :(得分:3)
您的XML代码包含:
<ImageView class="com.example.smokin4thomassullivan.CustomView" />
这将创建ImageView
,而不是CustomView
的实例。这意味着您的自定义onDraw()
方法永远不会被执行。您正在寻找的语法是:
<com.example.smokin4thomassullivan.CustomView />
class
属性仅适用于<view />
标记(只有当您想引用内部类时才需要它,因为它们的名称中包含$。)
答案 1 :(得分:1)
尝试此操作来测试ImageView的扩展:
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
System.out.println("onDraw");
Paint paint = new Paint(Paint.LINEAR_TEXT_FLAG);
paint.setColor(0xFFFF8844);
paint.setTextSize(12.0F);
canvas.drawText("Hello David!", 100, 100, paint);
}
摆脱onMeasure()
。
答案 2 :(得分:0)
我无法理解以这种方式做事的目的。使用ImageView顶部的TextView创建RelativeLayout会不会更简单?类似的东西:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="128dp"
android:layout_marginTop="51dp"
android:src="@android:drawable/btn_dialog" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView1"
android:layout_alignTop="@+id/imageView1"
android:layout_centerHorizontal="true"
android:text="TextView" />
</RelativeLayout>
甚至更好,将TextView放在LinearLayout中,并将图像设置为LinearLayout的背景?
编辑,回复评论:
这不是为了提高效率,而是试图将方形钉子放在圆孔中。它看起来好像你将一个对象 - 自定义ImageView - 设置为整个布局,然后尝试向单个对象(TextView)添加其他元素。尝试做的正是Layout对象的用途:包含其他对象的集合。在这种情况下,ImageViews和TextViews。
当然,我可能错过了这里的目的。如果这是一种不寻常的做事方式,那么我理解。但是,如果您只是试图在屏幕上显示内容,那么这似乎是一种非常复杂的方式。通常,命令setContentView(R.layout.activity_main);
设置一个XML文件,其根对象是布局对象的某个版本(LinearLayout,RelativeLayout等)。在该布局中,将是您要操作的对象。也许我们没有完整的MainActivity.xml?