我正在開發一本關於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