02-27 22:53:13.047: E/AndroidRuntime(11744): FATAL EXCEPTION: main
02-27 22:53:13.047: E/AndroidRuntime(11744): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.itcuties.android.reader/com.itcuties.android.reader.ItemDescriptionActivity}: java.lang.NullPointerException
02-27 22:53:13.047: E/AndroidRuntime(11744): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-27 22:53:13.047: E/AndroidRuntime(11744): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-27 22:53:13.047: E/AndroidRuntime(11744): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-27 22:53:13.047: E/AndroidRuntime(11744): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-27 22:53:13.047: E/AndroidRuntime(11744): at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 22:53:13.047: E/AndroidRuntime(11744): at android.os.Looper.loop(Looper.java:137)
02-27 22:53:13.047: E/AndroidRuntime(11744): at android.app.ActivityThread.main(ActivityThread.java:5041)
02-27 22:53:13.047: E/AndroidRuntime(11744): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 22:53:13.047: E/AndroidRuntime(11744): at java.lang.reflect.Method.invoke(Method.java:511)
02-27 22:53:13.047: E/AndroidRuntime(11744): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-27 22:53:13.047: E/AndroidRuntime(11744): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-27 22:53:13.047: E/AndroidRuntime(11744): at dalvik.system.NativeStart.main(Native Method)
02-27 22:53:13.047: E/AndroidRuntime(11744): Caused by: java.lang.NullPointerException
02-27 22:53:13.047: E/AndroidRuntime(11744): at com.itcuties.android.reader.ItemDescriptionActivity.onCreate(ItemDescriptionActivity.java:18)
02-27 22:53:13.047: E/AndroidRuntime(11744): at android.app.Activity.performCreate(Activity.java:5104)
02-27 22:53:13.047: E/AndroidRuntime(11744): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-27 22:53:13.047: E/AndroidRuntime(11744): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-27 22:53:13.047: E/AndroidRuntime(11744): ... 11 more
ItemDescriptionActivity.java
package com.itcuties.android.reader;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import com.itcuties.android.reader.R;
public class ItemDescriptionActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.item);
Bundle bundle = getIntent().getExtras();
String item_description = bundle.getString("description");
TextView desc = (TextView) findViewById(R.layout.item);
desc.setText(item_description);
}
}
应用程序获取rss数据,在列表视图中显示标题。我试图在点击时添加节目描述,但点击时应用程序崩溃。什么可能导致这些错误?
答案 0 :(得分:2)
您需要将id
传递给findViewById()
,或许您打算使用findViewById(R.id.item);
。如果仍然得到相同的LogCat错误,请确保布局item.xml
实际上有一个ID为@+id/item
的TextView。
此外,并非每个Activity都以Intent启动,并且并非每个Intent都有额外的内容,因此在使用它们之前,应确保这两个值都不是null
。
添加其他RSS值
让我们将链接与描述一起传递:
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
Intent intent = new Intent(activity, ItemDescriptionActivity.class);
RSSItem item = listItems.get(pos);
intent.putExtra("description", item.getDescription());
intent.putExtra("link", item.getLink());
activity.startActivity(intent);
}
接下来,我们将在ItemDescriptionActivity
:
String item_link;
最后设置Button以打开网页中的链接:
item_link = bundle.getString("link");
Button link = (Button) findViewById(R.id.button1);
link.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent browse = new Intent(Intent.ACTION_VIEW, Uri.parse(item_link));
startActivity(browse);
}
}