gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 15:05:29 24 4
gpt4 key购买 nike

enter image description here您好,我得到了为应用程序文本添加可选 TextView 选项的源代码。现在我想编辑这个源代码,我想在其中添加更多 TextView ,但是当我添加它时它会给我错误。所以我的问题是如何向以下源代码添加更多 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)

以及我的主要 Activity

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

最佳答案

您的 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!您不能在其中包含其他 View 。

所以您遇到的问题不是添加另一个 TextView ,而是在您的布局中错误地实现了这个特殊的 SelectableTextView,这就是您的应用程序崩溃的原因

关于java - 无法将 textview 添加到此 xml 代码中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22050639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com