所以,我遵循了this教程,以便我的应用可以接收内容。但是,当我尝试与我的应用程序分享内容时,它会崩溃。
以下是代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent);
}
else {
}
}
}
void handleSendText (Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
//Display definition. Use HTTP GET for Wikitonary.
CardUI mCardView = (CardUI) findViewById(R.id.cardsview);
mCardView.setSwipeable(true);
//mCardView.addCard(new MyCard("1 card"));
}
}
当它崩溃时,会抛出所有这些错误:
01-18 17:24:34.735: E/AndroidRuntime(1937): FATAL EXCEPTION: main
01-18 17:24:34.735: E/AndroidRuntime(1937): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dictionary/com.example.dictionary.MainActivity}: java.lang.NullPointerException
01-18 17:24:34.735: E/AndroidRuntime(1937): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-18 17:24:34.735: E/AndroidRuntime(1937): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-18 17:24:34.735: E/AndroidRuntime(1937): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-18 17:24:34.735: E/AndroidRuntime(1937): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-18 17:24:34.735: E/AndroidRuntime(1937): at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 17:24:34.735: E/AndroidRuntime(1937): at android.os.Looper.loop(Looper.java:137)
01-18 17:24:34.735: E/AndroidRuntime(1937): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-18 17:24:34.735: E/AndroidRuntime(1937): at java.lang.reflect.Method.invokeNative(Native Method)
01-18 17:24:34.735: E/AndroidRuntime(1937): at java.lang.reflect.Method.invoke(Method.java:511)
01-18 17:24:34.735: E/AndroidRuntime(1937): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-18 17:24:34.735: E/AndroidRuntime(1937): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-18 17:24:34.735: E/AndroidRuntime(1937): at dalvik.system.NativeStart.main(Native Method)
01-18 17:24:34.735: E/AndroidRuntime(1937): Caused by: java.lang.NullPointerException
01-18 17:24:34.735: E/AndroidRuntime(1937): at com.example.dictionary.MainActivity.handleSendText(MainActivity.java:42)
01-18 17:24:34.735: E/AndroidRuntime(1937): at com.example.dictionary.MainActivity.onCreate(MainActivity.java:29)
01-18 17:24:34.735: E/AndroidRuntime(1937): at android.app.Activity.performCreate(Activity.java:5104)
01-18 17:24:34.735: E/AndroidRuntime(1937): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-18 17:24:34.735: E/AndroidRuntime(1937): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-18 17:24:34.735: E/AndroidRuntime(1937): ... 11 more
01-18 17:24:43.078: I/Process(1937): Sending signal. PID: 1937 SIG: 9
有人能告诉我什么是错的吗?
答案 0 :(得分:2)
您的mCardView
似乎null
,因此您对mCardView.setSwipeable(true);
的调用将失败。
答案 1 :(得分:0)
它接合堆栈跟踪的帮助部分是:
01-18 17:24:34.735: E/AndroidRuntime(1937): at com.example.dictionary.MainActivity.handleSendText(MainActivity.java:42)
=>看看第42行。
如果您不喜欢调试器(或因某些原因无法使用调试器),请记录可疑变量的值以查找null
。