1
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

And 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);
    }
}

App is getting rss data, showing titles in list view. I am trying to add show description on click but app crashing on click. What can cause these errors?

1 Answer 1

2

You need to pass an id to findViewById(), perhaps you meant to use findViewById(R.id.item);. If you still get the same LogCat errors make sure the layout item.xml actually has a TextView with the id: @+id/item.

Also not every Activity is started with an Intent and not every Intent has extras, you should make sure that neither of these values are null before using them.


Adding another RSS value

Let's pass the link along with the description:

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);
}   

Next we'll create a new class variable inside ItemDescriptionActivity:

String item_link;

Finally set up the Button to open the link in a web page:

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);
    }
}
Sign up to request clarification or add additional context in comments.

12 Comments

fixed with findViewById(R.id.Description);
I'm pretty sure an activity is always started with an intent. I've never had this happen and I rely on it existing in my apps. If there's a case where the intent would be null I'd love to know to avoid breaking stuff :)
@Sam Thanks. Now i need to add item link to button on item.xml.
Ok, I looked through your code and let's use the Button you already set up. In onItemClick() pass the link value as well as the description: intent.putExtra("link", listItems.get(pos).getLink());, next in ItemDescriptionActivity read the link into a new variable, then find the Button, and set up an OnClickListener, finally use the in an Intent like: Android: Goto HTTP Url on Button Click
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.