改天,又一个错误,一个新程序员的生活啊。我正在尝试将 a 绘制bitmap
成TextView
orImageView
并且它不起作用。我一直在查看这段代码几个小时,试图找出它为什么不起作用。下面是代码。我没有收到任何错误,程序运行,它根本不显示位图
自定义视图.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`
谢谢你的帮助!