2016-06-10 73 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"> 
相關問題