2

我有一个可以正常工作的 LinearLayout,如下面的代码所示,但我想更改线性布局的顺序。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:weightSum="100"
    tools:context=".QuickNoteFragment" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="5dp" >

        <com.RiThBo.noter.ClearableEditText
            android:id="@+id/pn_et_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:drawableRight="@drawable/delete"
            android:hint="@string/qn_et_title_hint"
            android:inputType="textCapSentences|textMultiLine"
            android:paddingTop="5dp" />
    </LinearLayout>

    <com.RiThBo.noter.ClearableEditText
        android:id="@+id/pn_et_body"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawableRight="@drawable/delete"
        android:hint="@string/qn_et_body_hint"
        android:inputType="textCapSentences|textMultiLine" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="5dp"
        android:weightSum="100" >

        <CheckBox
            android:id="@+id/pn_cb_ongoing"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="99"
            android:text="@string/qn_cb_ongoing" />

        <Button
            android:id="@+id/pn_b_create"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/qn_b_create" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingBottom="5dp"
        android:paddingTop="10dp" >

        <Button
            android:id="@+id/pn_b_getPic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/pn_b_getPic" />

        <Button
            android:id="@+id/pn_b_takePic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/pn_b_takePic" />
    </LinearLayout>
 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingBottom="5dp"
        android:paddingTop="10dp" >
    <TextView
        android:id="@+id/pn_tv_preview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/pn_tv_preview" />

    <ImageView
        android:id="@+id/pn_iv_preview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/gallery_icon" />
     </LinearLayout>

</LinearLayout>

但是,当我将带有复选框和按钮的布局移动到底部时,我的应用程序强制关闭。

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:weightSum="100"
        tools:context=".Frag1" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingBottom="5dp" >

            <com.xxx.xxx.ClearableEditText
                android:id="@+id/pn_et_title"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:drawableRight="@drawable/delete"
                android:hint="@string/qn_et_title_hint"
                android:inputType="textCapSentences|textMultiLine"
                android:paddingTop="5dp" />
        </LinearLayout>

        <com.xxx.xxx.ClearableEditText
            android:id="@+id/pn_et_body"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:drawableRight="@drawable/delete"
            android:hint="@string/qn_et_body_hint"
            android:inputType="textCapSentences|textMultiLine" />       

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal"
            android:paddingBottom="5dp"
            android:paddingTop="10dp" >

            <Button
                android:id="@+id/pn_b_getPic"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/pn_b_getPic" />

            <Button
                android:id="@+id/pn_b_takePic"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/pn_b_takePic" />
        </LinearLayout>
     <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical"
            android:paddingBottom="5dp"
            android:paddingTop="10dp" >
        <TextView
            android:id="@+id/pn_tv_preview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/pn_tv_preview" />

        <ImageView
            android:id="@+id/pn_iv_preview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/gallery_icon" />
         </LinearLayout>

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingBottom="5dp"
            android:weightSum="100" >

            <CheckBox
                android:id="@+id/pn_cb_ongoing"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="99"
                android:text="@string/qn_cb_ongoing" />

            <Button
                android:id="@+id/pn_b_create"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/qn_b_create" />
        </LinearLayout>

    </LinearLayout>

我不知道为什么?

这是日志猫:

07-17 16:32:08.886: E/AndroidRuntime(19192): FATAL EXCEPTION: main
07-17 16:32:08.886: E/AndroidRuntime(19192): java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
07-17 16:32:08.886: E/AndroidRuntime(19192):    at com.RiThBo.noter.PicNoteFragment.onCreateView(PicNoteFragment.java:75)
07-17 16:32:08.886: E/AndroidRuntime(19192):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1470)
07-17 16:32:08.886: E/AndroidRuntime(19192):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:925)
07-17 16:32:08.886: E/AndroidRuntime(19192):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1102)
07-17 16:32:08.886: E/AndroidRuntime(19192):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-17 16:32:08.886: E/AndroidRuntime(19192):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1458)
07-17 16:32:08.886: E/AndroidRuntime(19192):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:438)
07-17 16:32:08.886: E/AndroidRuntime(19192):    at android.os.Handler.handleCallback(Handler.java:725)
07-17 16:32:08.886: E/AndroidRuntime(19192):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-17 16:32:08.886: E/AndroidRuntime(19192):    at android.os.Looper.loop(Looper.java:137)
07-17 16:32:08.886: E/AndroidRuntime(19192):    at android.app.ActivityThread.main(ActivityThread.java:5222)
07-17 16:32:08.886: E/AndroidRuntime(19192):    at java.lang.reflect.Method.invokeNative(Native Method)
07-17 16:32:08.886: E/AndroidRuntime(19192):    at java.lang.reflect.Method.invoke(Method.java:511)
07-17 16:32:08.886: E/AndroidRuntime(19192):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
07-17 16:32:08.886: E/AndroidRuntime(19192):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
07-17 16:32:08.886: E/AndroidRuntime(19192):    at dalvik.system.NativeStart.main(Native Method)

这是片段的 onCreateView():

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.pic_note, container, false);

        mImageBitmap = null;

        b_create = (Button) rootView.findViewById(R.id.pn_b_create);
        b_gallery = (Button) rootView.findViewById(R.id.pn_b_getPic);
        b_camera = (Button) rootView.findViewById(R.id.pn_b_takePic);
        et_body = (EditText) rootView.findViewById(R.id.pn_et_body);
        cb_ongoing = (CheckBox) rootView.findViewById(R.id.pn_cb_ongoing);

        et_title = (EditText) rootView.findViewById(R.id.pn_et_title);
        et_title.requestFocus();

        s_ticker_text = getString(R.string.ticker_text);
        s_subText = getString(R.string.sub_text);
        s_toastTitleText = getString(R.string.toast_missing_title);

        b_create.setOnClickListener(this);
        cb_ongoing.setOnClickListener(this);
        b_gallery.setOnClickListener(this);
        b_camera.setOnClickListener(this);

        cb_ongoing.setChecked(false);
        boolean_ongoing = false;

        iv_preview = (ImageView) rootView.findViewById(R.id.pn_iv_preview);
        iv_preview.setImageBitmap(null);
        iv_preview.setClickable(true);
        iv_preview.setOnClickListener(this);


        return rootView;
    }

谢谢

4

1 回答 1

1

编辑:真正的修复...进行清理并重建并尝试再次启动您的应用程序

根据您的堆栈跟踪:

android.widget.TextView cannot be cast to android.widget.Button

com.RiThBo.noter.PicNoteFragment.onCreateView(PicNoteFragment.java:75)

查看PicNoteFragment第 75 行的课程。或者发布您的 onCreateView() 代码,我们可以提供帮助。

于 2013-07-17T15:50:39.463 回答