我正在开发一本关于Android编程的书。当我运行第一个版本时它工作正常。然而,这本书然后将样式应用于列表项,并且当我查看模拟器时程序停止。它涉及一个扩展AppCompatTextView的类,它将每个列表项重新绘制为一个注释。当应用自定义主题时,应用程序意外停止
这是MainActiviy代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView myListView = (ListView) findViewById(R.id.myListView);
final EditText myEditText = (EditText) findViewById(R.id.myEditText);
final ImageButton myAddButton = (ImageButton) findViewById(R.id.myAddButton);
final ImageButton myDelButton = (ImageButton) findViewById(R.id.myDelButton);
final ImageButton myCanButton = (ImageButton) findViewById(R.id.myCanButton);
final ArrayList<String> toDoItems = new ArrayList<String>();
int resId = R.layout.activity_main;
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this, resId, toDoItems);
myEditText.setText("");
myListView.setAdapter(aa);
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
boolean result = getItemRecord(myListView, position);
if (result)
myListView.removeViewAt(position);
}
});
myAddButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
toDoItems.add(0, myEditText.getText().toString());
aa.notifyDataSetChanged();
myEditText.setText("");
}
});
}
带项目符号的行是唯一已更改的两行。他们涉及使用新班级。当我将它们改回到使用activity_main时,程序工作正常。
这里是TodoListItemView
public class TodoListItemView extends AppCompatTextView {
public TodoListItemView(Context context, AttributeSet attributeSet, int ds)
{
super(context, attributeSet, ds);
init();
}
public TodoListItemView(Context context) {
super(context);
init();
}
public TodoListItemView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
init();
}
private Paint marginPaint;
private Paint linePaint;
private int paperColor;
private float margin;
private void init() {
Resources myResources = getResources();
Context context = getContext();
marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
marginPaint.setColor(ContextCompat.getColor(context,
R.color.notepad_margin));
linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
linePaint.setColor(ContextCompat.getColor(context,
R.color.notepad_lines));
paperColor = ContextCompat.getColor(context, R.color.notepad_paper);
margin = myResources.getDimension(R.dimen.notepad_margin);
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawColor(paperColor);
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();
}
}
我有一种感觉它具有许多与结合子弹线的getColor方法做的代码。本书使用了不赞成使用的版本getColor(int),所以我用ContextCompat替换了它。代码可以构建,但应用程序在被输入到模拟器后不久会停止。
这里是styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="ToDoTheme" parent="@android:style/Theme.Black">
<item name="android:textSize">12sp</item>
</style>
用星号线(应该是粗体)定义自定义主题
一如往常,帮助是极大的赞赏。
后登录好友。 – Salman500