我已经仔细研究过这个问题,因为你们非常自信,我发现在我输入问题时我输入了错误的布局名称(对不起)
我拥有的正确内容是:
setContentView(R.layout.main);
结束修改
我在R.Java看到我的身份:
public static final int txtReadMore_link=0x7f06000e;
更新
03-19 17:05:07.158: E/AndroidRuntime(1356): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.website.playlist_activity}: java.lang.NullPointerException
03-19 17:05:07.158: E/AndroidRuntime(1356): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-19 17:05:07.158: E/AndroidRuntime(1356): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-19 17:05:07.158: E/AndroidRuntime(1356): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-19 17:05:07.158: E/AndroidRuntime(1356): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-19 17:05:07.158: E/AndroidRuntime(1356): at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 17:05:07.158: E/AndroidRuntime(1356): at android.os.Looper.loop(Looper.java:123)
03-19 17:05:07.158: E/AndroidRuntime(1356): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-19 17:05:07.158: E/AndroidRuntime(1356): at java.lang.reflect.Method.invokeNative(Native Method)
03-19 17:05:07.158: E/AndroidRuntime(1356): at java.lang.reflect.Method.invoke(Method.java:507)
03-19 17:05:07.158: E/AndroidRuntime(1356): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-19 17:05:07.158: E/AndroidRuntime(1356): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-19 17:05:07.158: E/AndroidRuntime(1356): at dalvik.system.NativeStart.main(Native Method)
03-19 17:05:07.158: E/AndroidRuntime(1356): Caused by: java.lang.NullPointerException
03-19 17:05:07.158: E/AndroidRuntime(1356): at net.website.playlist_activity.onCreate(playlist_activity.java:101)
03-19 17:05:07.158: E/AndroidRuntime(1356): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-19 17:05:07.158: E/AndroidRuntime(1356): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
我正在尝试从自定义列表视图访问textview,下面是代码,我得到空引用错误...我怎样才能访问txtReadMore_link
?
在用户点击链接时显示新活动。
playlist_activity
private TextView link;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_row);
//throwing error - NULL????
link = (TextView)findViewById(R.id.txtReadMore_link);
link.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int duration = Toast.LENGTH_SHORT;
TextView tx =(TextView)v.findViewById(R.id.txtReadMore_link);
Toast toast = Toast.makeText(playlist_activity.this, tx.getText(), duration);
toast.show();
}
});
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector"></ListView>
</LinearLayout>
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:background="@drawable/image_bg"
android:layout_marginRight="5dip">
<ImageView
android:id="@+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/rihanna"/>
</LinearLayout>
<!-- Title Of Song-->
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:text="Rihanna Love the way lie"
android:textColor="#040404"
android:typeface="sans"
android:textSize="15dip"
android:textStyle="bold"/>
<!-- Artist Name -->
<TextView
android:id="@+id/artist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:textColor="#343434"
android:textSize="10dip"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/thumbnail"
android:text="Just gona stand there and ..." />
<TextView
android:id="@+id/txtReadMore_link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:clickable="true"
android:text="Click here to read more.."
android:textSize="9sp" />
</RelativeLayout>
答案 0 :(得分:1)
只需清理你的项目并重新运行它:在Eclipse IDE中,单击 项目==&gt;清洁==&gt;检查您的项目==&gt;按OK 。并再次运行您的项目,它将工作;)
编辑:
您拥有NullPointerException
,因为在您的方法setContentView()
中,您已将活动的布局指定为:main.xml
您正在尝试找到View
按ID,该ID不在您的布局main.xml
中(位于您的list_row.xml
上),请确认您已指定了您的确切布局Activity
,或尝试在您的布局TextView
main.xml
答案 1 :(得分:0)
我怀疑您没有设置您认为属于活动的视图,即您确实
setContentView(R.layout.list_row);
在onCreate中,您可能想说
setContentView(R.layout.main);
由于txtReadMore_link
位于您的main.xml
,而不是list_row.xml
上,代码将无法找到该视图并返回null。
答案 2 :(得分:0)
您无法使用findViewById()
方法中的onCreate()
来搜索View
行布局中的ListView
。正确的方法是使用自定义适配器,并在getView()
方法中为该特定View
设置on click侦听器。围绕StackOverflow
有很多关于如何做到这一点的问题。以下是一些教程:
http://www.vogella.de/articles/AndroidListView/article.html或 http://commonsware.com/Android/excerpt.pdf