无法将textview添加到此xml代码中?

时间:2014-02-26 18:56:36

标签: java android eclipse layout textview

enter image description here嗨,我得到了一些源代码,用于为app的文本添加一个可选择的textview选项。现在我想编辑这个源代码,我想添加更多的文本视图,但是当我添加它时,它给了我错误。所以我的问题是如何在以下源代码中添加更多的文本视图。

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="#ffffff"
        >
    <com.zyz.mobile.example.ObservableScrollView
            android:id="@+id/scroller"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">
        <com.zyz.mobile.example.SelectableTextView
                android:id="@+id/main_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#000000"
                android:textSize="20sp"
                android:bufferType="spannable"
                android:text="@string/text">



          <TextView 
                android:id="@+id/main_text2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#000000"
                android:textSize="20sp"
                android:bufferType="spannable"
                android:text="@string/text"/>
          </com.zyz.mobile.example.SelectableTextView>
                 </com.zyz.mobile.example.ObservableScrollView>

Logcat输出:

02-27 00:19:37.241: E/AndroidRuntime(9783): FATAL EXCEPTION: main
    02-27 00:19:37.241: E/AndroidRuntime(9783): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zyz.mobile.example/com.zyz.mobile.example.MainActivity}: java.lang.ClassCastException: com.zyz.mobile.example.SelectableTextView cannot be cast to android.view.ViewGroup
    02-27 00:19:37.241: E/AndroidRuntime(9783):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
    02-27 00:19:37.241: E/AndroidRuntime(9783):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
    02-27 00:19:37.241: E/AndroidRuntime(9783):     at android.app.ActivityThread.access$700(ActivityThread.java:159)
    02-27 00:19:37.241: E/AndroidRuntime(9783):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
    02-27 00:19:37.241: E/AndroidRuntime(9783):     at android.os.Handler.dispatchMessage(Handler.java:99)
    02-27 00:19:37.241: E/AndroidRuntime(9783):     at android.os.Looper.loop(Looper.java:137)
    02-27 00:19:37.241: E/AndroidRuntime(9783):     at android.app.ActivityThread.main(ActivityThread.java:5419)
    02-27 00:19:37.241: E/AndroidRuntime(9783):     at java.lang.reflect.Method.invokeNative(Native Method)
    02-27 00:19:37.241: E/AndroidRuntime(9783):     at java.lang.reflect.Method.invoke(Method.java:525)
    02-27 00:19:37.241: E/AndroidRuntime(9783):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
    02-27 00:19:37.241: E/AndroidRuntime(9783):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
    02-27 00:19:37.241: E/AndroidRuntime(9783):     at dalvik.system.NativeStart.main(Native Method)
    02-27 00:19:37.241: E/AndroidRuntime(9783): Caused by: java.lang.ClassCastException: com.zyz.mobile.example.SelectableTextView cannot be cast to android.view.ViewGroup
    02-27 00:19:37.241: E/AndroidRuntime(9783):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:767)
    02-27 00:19:37.241: E/AndroidRuntime(9783):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
    02-27 00:19:37.241: E/AndroidRuntime(9783):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
    02-27 00:19:37.241: E/AndroidRuntime(9783):     at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
    02-27 00:19:37.241: E/AndroidRuntime(9783):     at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
    02-27 00:19:37.241: E/AndroidRuntime(9783):     at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
    02-27 00:19:37.241: E/AndroidRuntime(9783):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
    02-27 00:19:37.241: E/AndroidRuntime(9783):     at android.app.Activity.setContentView(Activity.java:1956)
    02-27 00:19:37.241: E/AndroidRuntime(9783):     at com.zyz.mobile.example.MainActivity.onCreate(MainActivity.java:21)
    02-27 00:19:37.241: E/AndroidRuntime(9783):     at android.app.Activity.performCreate(Activity.java:5372)
    02-27 00:19:37.241: E/AndroidRuntime(9783):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)

和我的主要活动

public class MainActivity extends Activity {

    private SelectableTextView mTextView;
    private int mTouchX;
    private int mTouchY;
    private final static int DEFAULT_SELECTION_LEN = 5;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // make sure the TextView's BufferType is Spannable, see the main.xml
        mTextView = (SelectableTextView) findViewById(R.id.main_text);
        mTextView.setDefaultSelectionColor(Color.YELLOW);


        mTextView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                showSelectionCursors(mTouchX, mTouchY);
                return true;
            }
        });
        mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTextView.hideCursor();

            }
        });
        mTextView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mTouchX = (int) event.getX();
                mTouchY = (int) event.getY();

                return false;
            }
        });
    }

    private void showSelectionCursors(int x, int y) {
        int start = mTextView.getPreciseOffset(x, y);

        if (start > -1) {
            int end = start + DEFAULT_SELECTION_LEN;
            if (end >= mTextView.getText().length()) {
                end = mTextView.getText().length() - 1;


            }
            mTextView.showSelectionControls(start, end);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

请在上面提及@ njzk2的评论 - SelectableTextView不是ViewGroup,因此它不能包含子View。您需要从TextView中删除SelectableTextView,然后拨打setText()上的SelectableTextView

另一个问题:我猜ObservableScrollView扩展了Android的原生ScrollView,对吧?如果这是真的,ObservableScrollView只能包含1个直接孩子。在这种情况下,您需要ViewGroup RelativeLayoutLinearLayout来保存其他内容。试试这个(让父母ObservableScrollView):

<?xml version="1.0" encoding="utf-8"?>
<com.zyz.mobile.example.ObservableScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scroller"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:orientation="vertical"
             android:background="#ffffff">

            <com.zyz.mobile.example.SelectableTextView
                android:id="@+id/main_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#000000"
                android:textSize="20sp"
                android:bufferType="spannable"
                android:text="@string/text"/>

        </LinearLayout>

</com.zyz.mobile.example.ObservableScrollView>

然后将ObservableScrollView添加到LinearLayout

答案 1 :(得分:0)

您的SelectableTextView不是ViewGroup的子类,因此您必须在最后用/>关闭标记,如:

   <com.zyz.mobile.example.SelectableTextView
            android:id="@+id/main_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:textSize="25sp"
            android:bufferType="spannable"
            android:text="@string/text"
            />

然后删除此部分,然后在底部:

  </com.zyz.mobile.example.SelectableTextView>

因为它不是ViewGroup!你不能在里面包含其他观点。

所以你遇到的问题是没有添加另一个textview,它会在你的布局中错误地实现这个特殊的SelectableTextView,这就是你的应用程序崩溃的原因