我试图在 Android 记事本应用程序的 editText 中获得水平线。一切都很好,但是当我单击添加注释错误时出现错误。
在图形布局中,它显示水平线,但当我在模拟器上调用它时不起作用。请建议。
06-25 11:52:07.980: D/gralloc_goldfish(2235): Emulator without GPU emulation detected.
06-25 11:52:11.178: D/AndroidRuntime(2235): Shutting down VM
06-25 11:52:11.178: W/dalvikvm(2235): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
06-25 11:52:11.449: E/AndroidRuntime(2235): FATAL EXCEPTION: main
06-25 11:52:11.449: E/AndroidRuntime(2235): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.sassypuma.todo.task/com.android.sassypuma.todo.task.Add_Task}: android.view.InflateException: Binary XML file line #3: Error inflating class com.android.sassypuma.todo.task.Add_Task.LinedEditText
06-25 11:52:11.449: E/AndroidRuntime(2235): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
06-25 11:52:11.449: E/AndroidRuntime(2235): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
06-25 11:52:11.449: E/AndroidRuntime(2235): at android.app.ActivityThread.access$600(ActivityThread.java:122)
06-25 11:52:11.449: E/AndroidRuntime(2235): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
06-25 11:52:11.449: E/AndroidRuntime(2235): at android.os.Handler.dispatchMessage(Handler.java:99)
06-25 11:52:11.449: E/AndroidRuntime(2235): at android.os.Looper.loop(Looper.java:137)
06-25 11:52:11.449: E/AndroidRuntime(2235): at android.app.ActivityThread.main(ActivityThread.java:4340)
06-25 11:52:11.449: E/AndroidRuntime(2235): at java.lang.reflect.Method.invokeNative(Native Method)
06-25 11:52:11.449: E/AndroidRuntime(2235): at java.lang.reflect.Method.invoke(Method.java:511)
06-25 11:52:11.449: E/AndroidRuntime(2235): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-25 11:52:11.449: E/AndroidRuntime(2235): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-25 11:52:11.449: E/AndroidRuntime(2235): at dalvik.system.NativeStart.main(Native Method)
06-25 11:52:11.449: E/AndroidRuntime(2235): Caused by: android.view.InflateException: Binary XML file line #3: Error inflating class com.android.sassypuma.todo.task.Add_Task.LinedEditText
06-25 11:52:11.449: E/AndroidRuntime(2235): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
06-25 11:52:11.449: E/AndroidRuntime(2235): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
06-25 11:52:11.449: E/AndroidRuntime(2235): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-25 11:52:11.449: E/AndroidRuntime(2235): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-25 11:52:11.449: E/AndroidRuntime(2235): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
06-25 11:52:11.449: E/AndroidRuntime(2235): at android.app.Activity.setContentView(Activity.java:1835)
06-25 11:52:11.449: E/AndroidRuntime(2235): at com.android.sassypuma.todo.task.Add_Task.onCreate(Add_Task.java:19)
06-25 11:52:11.449: E/AndroidRuntime(2235): at android.app.Activity.performCreate(Activity.java:4465)
06-25 11:52:11.449: E/AndroidRuntime(2235): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-25 11:52:11.449: E/AndroidRuntime(2235): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
06-25 11:52:11.449: E/AndroidRuntime(2235): ... 11 more
06-25 11:52:11.449: E/AndroidRuntime(2235): Caused by: java.lang.ClassNotFoundException: com.android.sassypuma.todo.task.Add_Task.LinedEditText
06-25 11:52:11.449: E/AndroidRuntime(2235): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
06-25 11:52:11.449: E/AndroidRuntime(2235): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
06-25 11:52:11.449: E/AndroidRuntime(2235): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
06-25 11:52:11.449: E/AndroidRuntime(2235): at android.view.LayoutInflater.createView(LayoutInflater.java:552)
06-25 11:52:11.449: E/AndroidRuntime(2235): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
06-25 11:52:11.449: E/AndroidRuntime(2235): ... 20 more
添加笔记活动:
public class Add_Task extends Activity{
private EditText description;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_task);
description = (EditText)findViewById(R.id.note);
}
}
内衬编辑文本类:
class LinedEditText extends EditText {
private Rect mRect;
private Paint mPaint;
// we need this constructor for LayoutInflater
public LinedEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public void init(){
mRect = new Rect();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setColor(0xFF668800); //SET YOUR OWN COLOR HERE
}
@Override
protected void onDraw(Canvas canvas) {
//int count = getLineCount();
int height = getHeight();
int line_height = getLineHeight();
int count = height / line_height;
if (getLineCount() > count)
count = getLineCount();//for long text with scrolling
Rect r = mRect;
Paint paint = mPaint;
int baseline = getLineBounds(0, r);//first line
for (int i = 0; i < count; i++) {
canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
baseline += getLineHeight();//next line
}
super.onDraw(canvas);
}
}
编辑:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.android.sassypuma.todo.task.LinedEditText
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/note"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:padding="5dip"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:gravity="top"
android:textSize="22sp"
android:lines="12"
android:capitalize="sentences" />
</LinearLayout>