Reputation: 20078
i have looked at the issue closely since you guys were so confident and i found that i have typed the incorrect layout name while i was typing in the question (sorry about that)
the correct contentview i have is this:
setContentView(R.layout.main);
End Edit
i see my id in R.Java:
public static final int txtReadMore_link=0x7f06000e;
Update:
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)
i am trying to access textview from the custom listview and below is the code and i am getting null ref error... how can i access to the txtReadMore_link
?
show the new activity when the user click on the 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>
Upvotes: 1
Views: 148
Reputation: 24181
just clean your project and re run it like this : in your Eclipse IDE , click Project ==> Clean ==> Check your Project ==> Press OK . and Run again your project and it will works ;)
EDIT :
you have the NullPointerException
because in your method setContentView()
, you have specified the layout of your activity as : main.xml
And you are trying to find a View
By id which is not in your layout main.xml
( it is on your list_row.xml
) , verify that you have specified the exact layout of your Activity
, or try to declare your TextView
in your layout main.xml
Upvotes: 1
Reputation: 87064
You can't use findViewById()
from the onCreate()
method to search for a View
that is in a ListView
row layout. The correct way is to use a custom adapter and in the getView()
method set the on click listener for that particular View
. There are many question around StackOverflow
regarding how to do this. Here are some tutorials:
http://www.vogella.de/articles/AndroidListView/article.html or http://commonsware.com/Android/excerpt.pdf
Upvotes: 0
Reputation: 17278
I suspect you are not setting the view you think you are for your activity, i.e. you do
setContentView(R.layout.list_row);
in the onCreate, where you probably meant to say
setContentView(R.layout.main);
As txtReadMore_link
is on your main.xml
, and not on your list_row.xml
, the code will not be able to find the view and return null.
Upvotes: 0