2016-06-10 54 views
1

我需要调试固定我的应用程序的帮助。的Android - java.lang.ClassCastException:android.widget.LinearLayout不能转换到android.widget.FrameLayout

app当我点击ok时发生崩溃。

这里是待办事项活性源代码

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)] 
     ); 
    } 
} 
} 

EDIT的一部分:

这是的logcat错误堆栈跟踪:

enter image description here

布局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> 
+1

你的问题是*太广*删除此,请你**编辑**它解释你试图调试它(你是否缩小了可能的故障位置?)以及你提供的任何最终的错误信息(如果有的话)? –

+0

@PatrickTrentin我添加了logcat的屏幕截图 –

+0

它说*“java.lang.ClassCastException:android.widget.LinearLayout不能转换为android.widget.FrameLayout”*,你的* original *源代码中的第164行和第141行是什么? –

回答

1

我固定它从布局上面

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
相关问题