0

改天,又一个错误,一个新程序员的生活啊。我正在尝试将 a 绘制bitmapTextVieworImageView并且它不起作用。我一直在查看这段代码几个小时,试图找出它为什么不起作用。下面是代码。我没有收到任何错误,程序运行,它根本不显示位图

自定义视图.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" />

日志猫

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`

谢谢你的帮助!

4

3 回答 3

3

您的 XML 代码包含:

<ImageView class="com.example.smokin4thomassullivan.CustomView" />

这将创建一个ImageView,而不是 的实例CustomView。这意味着您的自定义onDraw()方法将永远不会被执行。您正在寻找的语法是:

<com.example.smokin4thomassullivan.CustomView />

class属性仅适用于<view />标记(并且仅当您想要引用内部类时才需要它,因为它们的名称中包含 $。)

于 2013-03-23T00:11:45.487 回答
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().

于 2013-03-22T22:36:09.993 回答
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 文件,其根对象是某个版本的 Layout 对象(LinearLayout、RelativeLayout 等)。在该布局中将是您想要操作的对象。也许我们没有完整的 MainActivity.xml?

于 2013-03-22T22:35:03.343 回答