不能添加的TextView这个XML code? [英] cant add textview to this xml code?
问题描述
您好我得到我的源$ C $ C添加了一个可选择的TextView的选项文本应用程序。现在我想编辑,我想添加一些文字的观点,但是当我将其添加它给我的错误此源$ C $ C。所以我的问题是我怎么可以添加更多的textviews这个下面的源$ C $ C。
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:后台=#FFFFFF
>
< com.zyz.mobile.example.ObservableScrollView
机器人:ID =@ + ID /滚轮
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:fillViewport =真正的>
< com.zyz.mobile.example.SelectableTextView
机器人:ID =@ + ID / main_text
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:文字颜色=#000000
机器人:TEXTSIZE =20SP
机器人:bufferType =spannable
机器人:文本=@字符串/文本>
<的TextView
机器人:ID =@ + ID / main_text2
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:文字颜色=#000000
机器人:TEXTSIZE =20SP
机器人:bufferType =spannable
机器人:文本=@字符串/文本/>
< /com.zyz.mobile.example.SelectableTextView>
< /com.zyz.mobile.example.ObservableScrollView>
logcat的输出:
02-27 00:19:37.241:E / AndroidRuntime(9783):致命异常:主要
02-27 00:19:37.241:E / AndroidRuntime(9783):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.zyz.mobile.example / com.zyz.mobile.example.MainActivity}:java.lang中.ClassCastException:com.zyz.mobile.example.SelectableTextView不能转换为android.view.ViewGroup
02-27 00:19:37.241:E / AndroidRuntime(9783):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
02-27 00:19:37.241:E / AndroidRuntime(9783):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
02-27 00:19:37.241:E / AndroidRuntime(9783):在android.app.ActivityThread.access $ 700(ActivityThread.java:159)
02-27 00:19:37.241:E / AndroidRuntime(9783):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1316)
02-27 00:19:37.241:E / AndroidRuntime(9783):在android.os.Handler.dispatchMessage(Handler.java:99)
02-27 00:19:37.241:E / AndroidRuntime(9783):在android.os.Looper.loop(Looper.java:137)
02-27 00:19:37.241:E / AndroidRuntime(9783):在android.app.ActivityThread.main(ActivityThread.java:5419)
02-27 00:19:37.241:E / AndroidRuntime(9783):在java.lang.reflect.Method.invokeNative(本机方法)
02-27 00:19:37.241:E / AndroidRuntime(9783):在java.lang.reflect.Method.invoke(Method.java:525)
02-27 00:19:37.241:E / AndroidRuntime(9783):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1187)
02-27 00:19:37.241:E / AndroidRuntime(9783):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
02-27 00:19:37.241:E / AndroidRuntime(9783):在dalvik.system.NativeStart.main(本机方法)
02-27 00:19:37.241:E / AndroidRuntime(9783):由:java.lang.ClassCastException:com.zyz.mobile.example.SelectableTextView不能转换为android.view.ViewGroup
02-27 00:19:37.241:E / AndroidRuntime(9783):在android.view.LayoutInflater.rInflate(LayoutInflater.java:767)
02-27 00:19:37.241:E / AndroidRuntime(9783):在android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
02-27 00:19:37.241:E / AndroidRuntime(9783):在android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
02-27 00:19:37.241:E / AndroidRuntime(9783):在android.view.LayoutInflater.inflate(LayoutInflater.java:498)
02-27 00:19:37.241:E / AndroidRuntime(9783):在android.view.LayoutInflater.inflate(LayoutInflater.java:398)
02-27 00:19:37.241:E / AndroidRuntime(9783):在android.view.LayoutInflater.inflate(LayoutInflater.java:354)
02-27 00:19:37.241:E / AndroidRuntime(9783):在com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
02-27 00:19:37.241:E / AndroidRuntime(9783):在android.app.Activity.setContentView(Activity.java:1956)
02-27 00:19:37.241:E / AndroidRuntime(9783):在com.zyz.mobile.example.MainActivity.onCreate(MainActivity.java:21)
02-27 00:19:37.241:E / AndroidRuntime(9783):在android.app.Activity.performCreate(Activity.java:5372)
02-27 00:19:37.241:E / AndroidRuntime(9783):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
和我的主要活动
公共类MainActivity延伸活动{
私人SelectableTextView mTextView;
私人诠释mTouchX;
私人诠释mTouchY;
私人最终静态INT DEFAULT_SELECTION_LEN = 5;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
//确保TextView中的BufferType是Spannable,看到的main.xml
mTextView =(SelectableTextView)findViewById(R.id.main_text);
mTextView.setDefaultSelectionColor(Color.YELLOW);
mTextView.setOnLongClickListener(新View.OnLongClickListener(){
@覆盖
公共布尔onLongClick(视图v){
showSelectionCursors(mTouchX,mTouchY);
返回true;
}
});
mTextView.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
mTextView.hideCursor();
}
});
mTextView.setOnTouchListener(新View.OnTouchListener(){
@覆盖
公共布尔onTouch(视图V,MotionEvent事件){
mTouchX =(int)的event.getX();
mTouchY =(int)的event.getY();
返回false;
}
});
}
私人无效showSelectionCursors(INT X,int y)对{
INT开始= mTextView.get preciseOffset(X,Y);
如果(开始> -1){
INT端=启动+ DEFAULT_SELECTION_LEN;
如果(完> = mTextView.getText()长度()){
。年底= mTextView.getText()长度() - 1;
}
mTextView.showSelectionControls(开始,结束);
}
}
}
您SelectableTextView不是一个子类的ViewGroup,所以你必须用 /&GT关闭标签;
的结束这样的:
< com.zyz.mobile.example.SelectableTextView
机器人:ID =@ + ID / main_text
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:文字颜色=#000000
机器人:TEXTSIZE =25sp
机器人:bufferType =spannable
机器人:文本=@字符串/文本
/>
和去除这部分,然后在底部:
< /com.zyz.mobile.example.SelectableTextView>
,因为它不是一个的ViewGroup!你不能包含在它里面的其他意见。
让你不加入另一个TextView的问题,它是有这个特殊的SelectableTextView正确实现你的布局,这就是为什么你的应用程序崩溃
hi I get I source code for adding a selectable textview option for the text for app. now I want to edit this source code where I want to add some more text views but when I add it it gives me errors. so my question is how can I add more textviews to this following source code.
<?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 output:
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)
and my main 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);
}
}
}
your SelectableTextView is not a subclass ViewGroup so you have to close the tag with />
at the end like:
<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"
/>
and remove this part then at the bottom:
</com.zyz.mobile.example.SelectableTextView>
because it is not a ViewGroup! you cannot contain other views inside it.
so the problem that you have is not adding another textview, it is having this special SelectableTextView incorrectly implemented in your layout that is why your app crashes
这篇关于不能添加的TextView这个XML code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!