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?

milesh
  • 570
  • 3
  • 8
  • 19

1 Answers1

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);
    }
}
Sam
  • 86,580
  • 20
  • 181
  • 179
  • fixed with `findViewById(R.id.Description);` – milesh Feb 27 '13 at 21:31
  • 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 :) – dymmeh Feb 27 '13 at 21:41
  • Here is full code; https://github.com/m1lesh/ITCutiesReaderApp2/tree/ITCutiesReaderApp – milesh Feb 27 '13 at 21:43
  • @Sam Thanks. Now i need to add item link to button on item.xml. – milesh Feb 27 '13 at 21:48
  • @dymmeh It's rare when an Activity doesn't have an Intent. I know I have managed to do it in the past, but I may have shot _myself_ in the foot by deleting Intent manually... – Sam Feb 27 '13 at 21:48
  • @milesh What type of link? Perhaps web address like this: http://www.www.google.com. You can make those clickable automatically by adding [`android:autoLink="web"`](https://developer.android.com/reference/android/widget/TextView.html#attr_android:autoLink) to your TextView's XML. – Sam Feb 27 '13 at 21:51
  • @Sam The link of item that parsed from rss. – milesh Feb 27 '13 at 21:53
  • @milesh I'm sorry you want a Button to be a link, not a TextView. Please read: [Android: Goto HTTP Url on Button Click](http://stackoverflow.com/q/2762861/1267661) – Sam Feb 27 '13 at 21:57
  • @Sam Maybe TextView, doesn't matter. Which one is easier? Did you check the [code](http://github.com/m1lesh/ITCutiesReaderApp2/tree/ITCutiesReaderApp) ? – milesh Feb 27 '13 at 21:59
  • 1
    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](http://stackoverflow.com/q/2762861/1267661) – Sam Feb 27 '13 at 22:09
  • @Sam Ok, got first step. How will i set up an OnClickListener with link on button? – milesh Feb 27 '13 at 22:58
  • @Sam Can you check [this](http://stackoverflow.com/questions/15124924/android-classnotfoundexception-didnt-find-class) ? – milesh Feb 28 '13 at 00:00