English 中文(简体)
应用程序意外停止: 致命例外: MAIN
原标题:Application stopped unexpectedly: Fatal Exception: MAIN

我想使用 Spannanle 。 下面是我的活动文件 :

package com.Myapp;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.widget.EditText;
import android.widget.TextView;

public class MyappActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView vw = (TextView)findViewById(R.id.request);
        Spannable str = (Spannable)vw.getText();
        str.setSpan(new ForegroundColorSpan(Color.BLUE), 10, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


    }
}

我的主. xml 文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="73dp"
        android:layout_height="40dp"
        android:src="@drawable/logo" />

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="My APP"
        android:textColor="@color/black"
        android:textSize="19sp"
        android:typeface="serif" />

    <TextView
        android:id="@+id/request"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dip"
        android:text="I want to collect from" />
</Linearlayout>

我得到的错误是:

05-23 22:40:54.279: E/AndroidRuntime(451): FATAL EXCEPTION: main
05-23 22:40:54.279: E/AndroidRuntime(451): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Myapp/com.Myapp.MyappActivity}: java.lang.ClassCastException: java.lang.String
05-23 22:40:54.279: E/AndroidRuntime(451):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-23 22:40:54.279: E/AndroidRuntime(451):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-23 22:40:54.279: E/AndroidRuntime(451):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-23 22:40:54.279: E/AndroidRuntime(451):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-23 22:40:54.279: E/AndroidRuntime(451):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 22:40:54.279: E/AndroidRuntime(451):  at android.os.Looper.loop(Looper.java:130)
05-23 22:40:54.279: E/AndroidRuntime(451):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-23 22:40:54.279: E/AndroidRuntime(451):  at java.lang.reflect.Method.invokeNative(Native Method)
05-23 22:40:54.279: E/AndroidRuntime(451):  at java.lang.reflect.Method.invoke(Method.java:507)
05-23 22:40:54.279: E/AndroidRuntime(451):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-23 22:40:54.279: E/AndroidRuntime(451):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-23 22:40:54.279: E/AndroidRuntime(451):  at dalvik.system.NativeStart.main(Native Method)
05-23 22:40:54.279: E/AndroidRuntime(451): Caused by: java.lang.ClassCastException: java.lang.String
05-23 22:40:54.279: E/AndroidRuntime(451):  at com.Myapp.MyappActivity.onCreate(MyappActivity.java:21)
05-23 22:40:54.279: E/AndroidRuntime(451):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-23 22:40:54.279: E/AndroidRuntime(451):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

我漏掉了什么吗?

最佳回答

使用以下代码:

Spannable str = new SpannableString(vw.getText().toString());

或,

SpannableString str = new SpannableString(vw.getText().toString());

或,you can use a factory:

Spannable str = Spannable.Factory.getInstance().newSpannable(vw.getText());

您还需要在代码结尾处添加下行以获得效果 :

 vw.setText(str);

BTW, vw.getText () 或 vw.getText ().to String () 两种工作都没有问题:)

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签