我需要调试和修复我的应用程序的帮助。
点击ok
时, app 崩溃了。
以下是 ToDo活动的源代码的一部分
public class ToDoActivity extends RealmBaseActivity {
private static final int[] COLORS = new int[] {
Color.argb(255, 28, 160, 170),
Color.argb(255, 99, 161, 247),
Color.argb(255, 13, 79, 139),
Color.argb(255, 89, 113, 173),
Color.argb(255, 200, 213, 219),
Color.argb(255, 99, 214, 74),
Color.argb(255, 205, 92, 92),
Color.argb(255, 105, 5, 98)
};
private Realm realm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.to_do_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buildAndShowInputDialog();
}
});
resetRealm();
realm = Realm.getInstance(getRealmConfig());
RealmResults<TodoItem> toDoItems = realm
.where(TodoItem.class)
.findAllSorted("id", Sort.ASCENDING);
ToDoRealmAdapter toDoRealmAdapter =
new ToDoRealmAdapter(this, toDoItems, true, true);
RealmRecyclerView realmRecyclerView =
(RealmRecyclerView) findViewById(R.id.realm_recycler_view);
realmRecyclerView.setAdapter(toDoRealmAdapter);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (realm != null) {
realm.close();
realm = null;
}
}
private void buildAndShowInputDialog() {
final AlertDialog.Builder builder = new
AlertDialog.Builder(ToDoActivity.this);
builder.setTitle("Create A Task");
LayoutInflater li = LayoutInflater.from(this);
View dialogView = li.inflate(R.layout.to_do_dialog_view, null);
final EditText input = (EditText) dialogView.findViewById(R.id.input);
builder.setView(dialogView);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
addToDoItem(input.getText().toString());
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
final AlertDialog dialog = builder.show();
input.setOnEditorActionListener(
new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE ||
(event.getAction() == KeyEvent.ACTION_DOWN &&
event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
dialog.dismiss();
addToDoItem(input.getText().toString());
return true;
}
return false;
}
});
}
private void addToDoItem(String toDoItemText) {
if (toDoItemText == null || toDoItemText.length() == 0) {
Toast
.makeText(this, "Empty ToDos don't get stuff done!", Toast.LENGTH_SHORT)
.show();
return;
}
realm.beginTransaction();
TodoItem todoItem = realm.createObject(TodoItem.class);
todoItem.setId(System.currentTimeMillis());
todoItem.setDescription(toDoItemText);
realm.commitTransaction();
}
public class ToDoRealmAdapter
extends RealmBasedRecyclerViewAdapter<TodoItem, ToDoRealmAdapter.ViewHolder> {
public class ViewHolder extends RealmViewHolder {
public TextView todoTextView;
public ViewHolder(FrameLayout container) {
super(container);
this.todoTextView = (TextView) container.findViewById(R.id.todo_text_view);
}
}
public ToDoRealmAdapter(
Context context,
RealmResults<TodoItem> realmResults,
boolean automaticUpdate,
boolean animateResults) {
super(context, realmResults, automaticUpdate, animateResults);
}
@Override
public ViewHolder onCreateRealmViewHolder(ViewGroup viewGroup, int viewType) {
View v = inflater.inflate(R.layout.to_do_item_view, viewGroup, false);
return new ViewHolder((FrameLayout) v);
}
@Override
public void onBindRealmViewHolder(ViewHolder viewHolder, int position) {
final TodoItem toDoItem = realmResults.get(position);
viewHolder.todoTextView.setText(toDoItem.getDescription());
viewHolder.itemView.setBackgroundColor(
COLORS[(int) (toDoItem.getId() % COLORS.length)]
);
}
}
}
编辑:
这是 logcat 中的错误堆栈跟踪:
布局to_do_item_view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/todo_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="4dp"
android:textSize="32sp"
android:textColor="@android:color/white"
/>
</FrameLayout>
</LinearLayout>
答案 0 :(得分:1)
我通过从上面的布局中删除它来修复它
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">