我正在开发加密和解密应用程序。该应用程序有2个按钮:第一个用于Decrypt文件,第二个用于Decrypty消息。 两个按钮都在相同的活动中执行i:e DecryptActivity.java
当我按下Decrypt File按钮时,它会顺利运行,但是当我按Decrypt Message按钮时,log cat会显示错误:
07-24 13:09:26.732: W/dalvikvm(27183): threadid=1: thread exiting with uncaught exception (group=0x419aeba8)
07-24 13:09:26.732: E/AndroidRuntime(27183): FATAL EXCEPTION: main
07-24 13:09:26.732: E/AndroidRuntime(27183): Process: gargdk.android.mailencryption, PID: 27183
07-24 13:09:26.732: E/AndroidRuntime(27183): java.lang.RuntimeException: Unable to start activity ComponentInfo{gargdk.android.mailencryption/gargdk.android.mailencryption.DecryptActivity}: java.lang.NullPointerException
07-24 13:09:26.732: E/AndroidRuntime(27183): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
07-24 13:09:26.732: E/AndroidRuntime(27183): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
07-24 13:09:26.732: E/AndroidRuntime(27183): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-24 13:09:26.732: E/AndroidRuntime(27183): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-24 13:09:26.732: E/AndroidRuntime(27183): at android.os.Handler.dispatchMessage(Handler.java:102)
07-24 13:09:26.732: E/AndroidRuntime(27183): at android.os.Looper.loop(Looper.java:136)
07-24 13:09:26.732: E/AndroidRuntime(27183): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-24 13:09:26.732: E/AndroidRuntime(27183): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 13:09:26.732: E/AndroidRuntime(27183): at java.lang.reflect.Method.invoke(Method.java:515)
07-24 13:09:26.732: E/AndroidRuntime(27183): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-24 13:09:26.732: E/AndroidRuntime(27183): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-24 13:09:26.732: E/AndroidRuntime(27183): at dalvik.system.NativeStart.main(Native Method)
07-24 13:09:26.732: E/AndroidRuntime(27183): Caused by: java.lang.NullPointerException
07-24 13:09:26.732: E/AndroidRuntime(27183): at java.util.regex.Matcher.reset(Matcher.java:177)
07-24 13:09:26.732: E/AndroidRuntime(27183): at java.util.regex.Matcher.<init>(Matcher.java:90)
07-24 13:09:26.732: E/AndroidRuntime(27183): at java.util.regex.Pattern.matcher(Pattern.java:297)
07-24 13:09:26.732: E/AndroidRuntime(27183): at gargdk.android.mailencryption.DecryptActivity.onCreate(DecryptActivity.java:260)
07-24 13:09:26.732: E/AndroidRuntime(27183): at android.app.Activity.performCreate(Activity.java:5231)
07-24 13:09:26.732: E/AndroidRuntime(27183): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-24 13:09:26.732: E/AndroidRuntime(27183): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
07-24 13:09:26.732: E/AndroidRuntime(27183): ... 11 more
我已经检查了其他帖子,但错误没有解决。 Android Manifesto文件已经有了条目。
Android Manisfesto文件:
<activity
android:name="gargdk.android.mailencryption.DecryptActivity"
android:label="@string/title_decrypt"
android:configChanges="keyboardHidden|orientation|keyboard">
<intent-filter>
<action android:name="gargdk.android.mailencryption.intent.DECRYPT" />
<action android:name="gargdk.android.mailencryption.intent.DECRYPT_FILE" />
<action android:name="gargdk.android.mailencryption.intent.DECRYPT_AND_RETURN" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="*/*"/>
</intent-filter>
</activity>
DecryptActivity.java代码
256 if (mSource.getCurrentView().getId() == R.id.sourceMessage &&
257 mMessage.getText().length() == 0) {
258 ClipboardManager clip = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
259 String data = "";
260 Matcher matcher = Apg.PGP_MESSAGE.matcher(clip.getText());
261 if (!matcher.matches()) {
262 matcher = Apg.PGP_SIGNED_MESSAGE.matcher(clip.getText());
263 }
264 if (matcher.matches()) {
265 data = matcher.group(1);
266 mMessage.setText(data);
267 Toast.makeText(this, R.string.usingClipboardContent,
267 Toast.LENGTH_SHORT).show();
268 }
269 }
答案 0 :(得分:0)
如果剪贴板中没有文字,clip.getText()
可以返回null
。要在此处避免空指针,请调用clip.hasText()
以查看剪贴板中是否有某些文本。 Android docs
例如:
if(clip.hasText()) {
// Do stuff with clip
}