我试图在我的 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>