0

我试图在我的 android 应用程序中从我的 java 代码中删除一个文本,但我得到了Force Close. 我正在使用2.3.1模拟器,我正在使用以下代码进行攻击

TextView tvTxt=(TextView)findViewById(R.id.textView1);
tvTxt.setPaintFlags(tvTxt.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

请帮我解决这个难题。

提前致谢。

这是我的 Logcat

07-12 17:19:41.172: E/AndroidRuntime(1777): FATAL EXCEPTION: main
07-12 17:19:41.172: E/AndroidRuntime(1777): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.colors.assasin/com.colors.assasin.MainActivity}: java.lang.NullPointerException
07-12 17:19:41.172: E/AndroidRuntime(1777):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
07-12 17:19:41.172: E/AndroidRuntime(1777):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
07-12 17:19:41.172: E/AndroidRuntime(1777):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-12 17:19:41.172: E/AndroidRuntime(1777):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
07-12 17:19:41.172: E/AndroidRuntime(1777):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 17:19:41.172: E/AndroidRuntime(1777):     at android.os.Looper.loop(Looper.java:123)
07-12 17:19:41.172: E/AndroidRuntime(1777):     at android.app.ActivityThread.main(ActivityThread.java:3647)
07-12 17:19:41.172: E/AndroidRuntime(1777):     at java.lang.reflect.Method.invokeNative(Native Method)
07-12 17:19:41.172: E/AndroidRuntime(1777):     at java.lang.reflect.Method.invoke(Method.java:507)
07-12 17:19:41.172: E/AndroidRuntime(1777):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-12 17:19:41.172: E/AndroidRuntime(1777):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-12 17:19:41.172: E/AndroidRuntime(1777):     at dalvik.system.NativeStart.main(Native Method)
07-12 17:19:41.172: E/AndroidRuntime(1777): Caused by: java.lang.NullPointerException
07-12 17:19:41.172: E/AndroidRuntime(1777):     at com.colors.assasin.MainActivity.onCreate(MainActivity.java:27)
07-12 17:19:41.172: E/AndroidRuntime(1777):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-12 17:19:41.172: E/AndroidRuntime(1777):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)

NullPointer上升Logcat线为

Caused by: java.lang.NullPointerException at com.colors.assasin.MainActivity.onCreate(MainActivity.java:27)

tvTxt.setPaintFlags(tvTxt.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

MainActivity.java的很简单

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView our_text=(TextView)findViewById(R.id.textView1);
    our_text.setPaintFlags(our_text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

    }
}

的代码activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="TextView" />

</RelativeLayout>
4

2 回答 2

1

使用 SpannableString 获得 StrikeThrough 效果。

private SpannableString strikeThrough(String text) {
    SpannableString spannableString = new SpannableString(text);
    spannableString.setSpan(new StrikethroughSpan(), 0, text.length(), 0);
    return spannableString;
}

TextView our_text = (TextView) findViewById(R.id.our_text);
our_text.setText(strikeThrough((String) our_text.getText()));
于 2013-07-12T13:25:21.500 回答
0

好的 - 我不知道发生了什么......但我不想删除它,以便其他人可以看到评论。我测试过

TextView t=new TextView(mContext);
t.setPaintFlags(t.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

它抛出了一个NPE。我把它改成

TextView t=new TextView(mContext);
t.setText("t");
t.setPaintFlags(t.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

但事实并非如此。我把它改成

TextView t=new TextView(mContext);
t.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);

它没有,最后我把它改回

TextView t=new TextView(mContext);
t.setPaintFlags(t.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

但事实并非如此。

在执行 getPaintFlags 之前,您必须在 textview 上设置paintflags,否则您将获得 NPE。所以也许你应该使用

our_text.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG) 

if(our_text.getPaintFlags()!=null){
our_text.setPaintFlags(our_text.getPaintFlags()|Paint.STRIKE_THRU_TEXT_FLAG)
}
 else {
our_text.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG)
}

于 2013-07-12T12:08:59.120 回答