我正在尝试创建一个待办事项列表应用。我将输入的文本添加到输入的列表项中,它没有样式就可以正常工作。 使用paint添加样式并绘制其给出空指针异常后。我正在初始化" ToDoListItemView"的init方法中的所有绘图和绘制对象。我试过" log.i"但它没有进入ToDoListItemView的init方法。
这是我的代码。
MainActivity:
package todo.prac.com.todolist;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView mylistView=(ListView)findViewById(R.id.myListView);
final EditText editText=(EditText)findViewById(R.id.myEditText);
final ArrayList<String>todoItems=new ArrayList<String>();
final ArrayAdapter<String>adapter;
adapter=new ArrayAdapter<String>(this,R.layout.todolist_view,todoItems);
mylistView.setAdapter(adapter);
editText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if(keyEvent.getAction()==KeyEvent.ACTION_DOWN){
if(i==KeyEvent.KEYCODE_ENTER){
todoItems.add(0,editText.getText().toString());
adapter.notifyDataSetChanged();
editText.setText("");
return true;
}
}
return false;
}
});
}
}
和我的ToDoListItemView
package todo.prac.com.todolist;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
public class ToDoListItemView extends TextView {
private Paint marginPaint;
private Paint linePaint;
private int paperColor;
private float margin;
public ToDoListItemView(Context context) {
super(context);
}
public ToDoListItemView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ToDoListItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ToDoListItemView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
private void init(){
Log.i("init","inside");
Resources myResources=getResources();
marginPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
linePaint=new Paint(Paint.ANTI_ALIAS_FLAG);
marginPaint.setColor(myResources.getColor(R.color.notepad_margin));
linePaint.setColor(myResources.getColor(R.color.notepad_line));
paperColor=myResources.getColor(R.color.notepad_paper);
margin=myResources.getDimension(R.dimen.notepad_margin);
}
@Override
public void onDraw(Canvas canvas){
canvas.drawColor(paperColor);
if(linePaint==null){
Log.i("linepaint","is null");
}
canvas.drawLine(0,0,getMeasuredHeight(),0,linePaint);
canvas.drawLine(0,getMeasuredHeight(),getMeasuredWidth(),getMeasuredHeight(),linePaint);
canvas.drawLine(margin,0,margin,getMeasuredHeight(),marginPaint);
canvas.save();
canvas.translate(margin,0);
super.onDraw(canvas);
canvas.restore();
}
}
和我的TodoList布局xml:
<?xml version="1.0" encoding="utf-8"?>
<todo.prac.com.todolist.ToDoListItemView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:padding="10dp"
android:scrollbars="vertical"
android:textColor="@color/notepad_text"
android:fadingEdge="vertical"
/>
答案 0 :(得分:5)
您没有在类的构造函数中调用init()方法。
public ToDoListItemView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ToDoListItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ToDoListItemView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
答案 1 :(得分:1)
我通过这种方法解决了这个问题。
public ToDoListItemView(Context context) {
this(context,null);
}
public ToDoListItemView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public ToDoListItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// initiate your some things
}
注意:只有一个超级。其他人代替了这一点。