我有一个完全适用于API 15-19的应用程序,我在将其迁移到AppCompat时遇到了问题:当我将MainActivity extends FragmentActivity
更改为MainActivity extends AppCompatActivity
时,编译失败(我也会更新所有相应的库并将getFragmentManager()
替换为getSupportFragmentManager()
)。试图找出导致这个错误的原因我发现当ListView的适配器填充其中一个项目按钮(即第一个按钮id="@+id/time_field"
)时会发生这种情况:
listitem_task.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_layout"
android:paddingBottom="5dp">
<Button
android:id="@+id/time_field"
android:layout_width="115dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp"
android:textColor="@color/black_col"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:singleLine="true"
android:textSize="18sp"
android:longClickable="false"
android:enabled="true"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:layout_alignParentBottom="false" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/task_field"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/time_field"
android:layout_toEndOf="@+id/time_field"
android:textColor="@color/black_col"
android:gravity="start|center"
android:paddingLeft="10dp"
android:clickable="false"
android:enabled="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:longClickable="false"
android:paddingRight="10dp"
android:maxLines="2" />
</RelativeLayout>
因此,当我在ListView 适配器(// FORMATTING TIME-FIELD:
之后的行中)注释掉负责格式化此按钮的行时,这个错误不会发生:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import java.util.ArrayList;
public class TaskAdapter extends BaseAdapter {
private ArrayList<Task> taskList;
private Context context;
public TaskAdapter(ArrayList<Task> taskList, Context context) {
this.taskList = taskList;
this.context = context;
}
@Override
public int getCount() {
return taskList.size();
}
@Override
public Object getItem(int position) {
return taskList.get(position);
}
@Override
public long getItemId(int itemId) {
return itemId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Task task = taskList.get(position);
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.listitem_task, parent, false);
}
int taskHrsEnd = task.getHrsEnd();
int taskHrsStart = task.getHrsStart();
int taskMinsStart = task.getMinsStart();
int taskMinsEnd = task.getMinsEnd();
Button taskField = (Button) convertView.findViewById(R.id.task_field);
taskField.setText(task.getName() + " (" + task.getDurationMins() + "m)");
Button timeField = (Button) convertView.findViewById(R.id.time_field);
if (task.isChecked()) {
taskField.setBackgroundResource(R.color.transparent_white_col);
timeField.setBackgroundResource(R.color.transparent_white_col);
} else {
taskField.setBackgroundResource(R.color.white_col);
timeField.setBackgroundResource(R.color.white_col);
}
// FORMATTING TIME-FIELD:
if (taskHrsStart < 10 && taskMinsStart > 9) {
timeField.setText("0" + taskHrsStart + ":" + taskMinsStart);
}
if (taskHrsStart > 9 && taskMinsStart < 10) {
timeField.setText(taskHrsStart + ":0" + taskMinsStart);
}
if (taskHrsStart > 9 && taskMinsStart > 9) {
timeField.setText(taskHrsStart + ":" + taskMinsStart);
}
if (taskHrsStart < 10 && taskMinsStart < 10) {
timeField.setText("0" + taskHrsStart + ":0" + taskMinsStart);
}
if (taskHrsEnd < 10 && taskMinsEnd > 9) {
timeField.append(" - " + "0" + taskHrsEnd + ":" + taskMinsEnd);
}
if (taskHrsEnd > 9 && taskMinsEnd < 10) {
timeField.append(" - " + taskHrsEnd + ":0" + taskMinsEnd);
}
if (taskHrsEnd > 9 && taskMinsEnd > 9) {
timeField.append(" - " + taskHrsEnd + ":" + taskMinsEnd);
}
if (taskHrsEnd < 10 && taskMinsEnd < 10) {
timeField.append(" - " + "0" + taskHrsEnd + ":0" + task.getMinsEnd());
}
return convertView;
}
}
我从 MainActivity.java 中调用它:
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>, AdapterView.OnItemClickListener {
private ArrayList<Task> taskList;
private ListView listView;
private TaskAdapter taskAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init() {
findViewById(R.id.btn_back).setVisibility(View.GONE);
findViewById(R.id.btn_delete_selected).setVisibility(View.GONE);
taskList = new ArrayList<>();
viewTimeRemain = (TextView) findViewById(R.id.time_remain_view);
listView = (ListView) findViewById(R.id.task_list);
listView.setOnItemClickListener(this);
registerForContextMenu(listView);
taskAdapter = new TaskAdapter(taskList, this);
}
public void addTask(View view) {
EditText taskName = (EditText) findViewById(R.id.taskName);
EditText taskDuration = (EditText) findViewById(R.id.taskDuration);
Task task = new Task();
task.setName(taskName.getText().toString());
task.setDurationMins(Integer.parseInt(taskDuration.getText().toString()));
setTaskTimeInterval(task);
taskList.add(task);
fillList(taskAdapter);
private void fillList(TaskAdapter taskAdapter) {
listView.setAdapter(taskAdapter);
}
}
崩溃报告:
java.lang.IndexOutOfBoundsException
at android.graphics.Paint.getTextRunAdvances(Paint.java:1879)
at android.text.TextLine.handleText(TextLine.java:747)
at android.text.TextLine.handleRun(TextLine.java:898)
at android.text.TextLine.measureRun(TextLine.java:414)
at android.text.TextLine.measure(TextLine.java:293)
at android.text.TextLine.metrics(TextLine.java:267)
at android.text.Layout.getLineExtent(Layout.java:976)
at android.text.Layout.getLineMax(Layout.java:932)
at android.text.Layout.getLineLeft(Layout.java:889)
at android.widget.TextView.bringTextIntoView(TextView.java:6652)
at android.widget.TextView.onPreDraw(TextView.java:4696)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1867)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
请帮我理解这个问题!谢谢!
答案 0 :(得分:0)
我发现当我尝试将.append()文本添加到TextView(Button)时会发生这种错误。所以我使用StringBuffer来构造我需要的文本,然后将这个文本设置为我的TextView(不附加),它现在可以正常工作。