我有一個應用程序在API 15-19上工作完美,並且在將它移植到AppCompat時遇到問題:當我將MainActivity extends FragmentActivity
更改爲MainActivity extends AppCompatActivity
時,編譯失敗(也是I更新所有適當的庫並用getSupportFragmentManager()
替換getFragmentManager()
)。試圖找出是什麼原因導致這個錯誤,我發現它時,ListView的適配器填充項按鈕(即第一按鈕id="@+id/time_field"
)之一發生:ListView適配器不適用於AppCompatActivity
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>
所以,這當我(// FORMATTING TIME-FIELD:
後線)註釋掉負責線在ListView格式化這個按鈕適配器錯誤不會發生:
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)
請幫我理解這個問題!謝謝!
從哪裏調用TaskAdapter?你能更新嗎? – user3515851
你好!謝謝你的答案!我從MainActivity調用它。我已將它添加到帖子中! – userVadim
我剛剛發現這個錯誤發生在我試圖.append()文本到TextView(Button)時。所以我使用了StringBuffer來構建我需要的文本,然後將此文本設置爲我的TextView(無需追加),現在它可以工作。嗯,有趣的是,爲什麼在使用AppCompat時,文本無法附加到TextView(Button)? – userVadim