user3082271
user3082271

Reputation: 127

android start activity error

I'm working in an app which displays a list of objects and, when you click into one, it should shift to a screen displaying the details: the app crashes on that moment yet the spot where the error is at feels odd and I don't catch the cause of the crash.

Here's the code for my activity EventList, linked to a class named Item Event:

Event List

public class EventList extends Activity implements OnClickListener {
        ImageView bookmark;
        ImageView palau;
        ImageView noimage;

    TextView nom;
    TextView cognom;
    TextView data;
    TextView descripcio;

    Button map;
    Button join;

    Context ct;
    ItemEvent item;

        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_list);

        item = (ItemEvent) getIntent().getSerializableExtra("item");
        ct = this;
        Toast.makeText(this, item.getNom(), Toast.LENGTH_LONG).show();
        element_confi(); //line 63!
                        }

        public void element_confi() {
            bookmark = (ImageView) findViewById(R.id.bookmark);
            nom = (TextView) findViewById(R.id.nom);
            data= (TextView) findViewById(R.id.date);
            descripcio = (TextView) findViewById(R.id.descripcio);
            map = (Button) findViewById(R.id.mapa);
            join = (Button) findViewById(R.id.join);
            nom.setText(item.getNom());
            data.setText(item.getDate()); //line 76!
            descripcio.setText(item.getDescription());

    if( item.getBookmarks()){
        bookmark.setImageResource(R.drawable.favorito1);//line 80!
    }
    else{
        bookmark.setImageResource(R.drawable.favorito);
    }


            map.setOnClickListener(this);
            join.setOnClickListener(this);
            ViewPager viewPager= (ViewPager) findViewById(R.id.view_pager);
            ImageAdapter adapter = new ImageAdapter(item.getRutaFoto1(),item.getRutaFoto2());
            viewPager.setAdapter(adapter);
        }
}

CLass Item Event

public class ItemEvent implements Serializable{

    protected long id;
    protected int rutaFoto1;
    protected int rutaFoto2;
    protected String description;
    protected String nom;
    protected String date;
    protected String place;
    protected Boolean bookmark;

    public ItemEvent() {
        this.nom = "";
        this.date = "";
        this.place = "";
        this.rutaFoto1 = R.drawable.no_image;
    }

    public ItemEvent(long id, String nom, String date) {
        this.id = id;
        this.nom = nom;
        this.date = date;
        this.rutaFoto1 = R.drawable.no_image;
    }

    public ItemEvent(long id, String nom, String place, String date, String bookmark,
            Boolean bookmarks, int rutaFoto1, int rutaFoto2) {
        this.id = id;
        this.nom = nom;
        this.place = place;
        this.date = date;
        this.description= description;
        this.bookmark = bookmarks;
        this.rutaFoto1 = rutaFoto1;
        this.rutaFoto2 = rutaFoto2;

    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public int getRutaFoto1() {
        return rutaFoto1;
    }

    public void setRutaFoto1(int rutaFoto1) {
        this.rutaFoto1 = rutaFoto1;
    }

    public int getRutaFoto2() {
        return rutaFoto2;
    }

    public void setRutaFoto2(int rutaFoto2) {
        this.rutaFoto2 = rutaFoto2;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getPlace() {
        return place;
    }

    public void setPlace(String place) {
        this.place = place;
    }


    public Boolean getBookmarks() {
        return bookmark;
    }

    public void setBookmark(Boolean bookmark) {
        this.bookmark = bookmark;
    }


}

Plus another class, an adapter.

Item Event Adapter

public class ItemEventAdapter extends BaseAdapter{

    protected Activity activity;
      protected ArrayList<ItemEvent> items;

      public ItemEventAdapter(Activity activity, ArrayList<ItemEvent> items) {
        this.activity = activity;
        this.items = items;
      }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return items.size();
    }

     @Override
      public Object getItem(int position) {
        return items.get(position);
      }

      @Override
      public long getItemId(int position) {
        return items.get(position).getId();
      }

      @Override
      public View getView(int position, View contentView, ViewGroup parent) {
        View vi=contentView;

        if(contentView == null) {
          LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          vi = inflater.inflate(R.layout.item_layout, null);
        }

        ItemEvent item = items.get(position);


        ImageView bookmarked = (ImageView) vi.findViewById(R.id.img_boomark);
        if(item.bookmark==true){
            bookmarked.setImageResource(R.drawable.favorito1);
        }

        else {
            bookmarked.setImageResource(R.drawable.favorito);
        }

        ImageView foto = (ImageView) vi.findViewById(R.id.image);
        foto.setImageResource(item.getRutaFoto1());

        TextView nom = (TextView) vi.findViewById(R.id.nom);
        nom.setText(item.getNom());

        TextView data = (TextView) vi.findViewById(R.id.data);
        data.setText(item.getDate());

        TextView lloc= (TextView) vi.findViewById(R.id.place);
        lloc.setText(item.getPlace());

        return vi;
      }


}

Here's my LogCat:

12-11 23:02:05.938: E/AndroidRuntime(4215): FATAL EXCEPTION: main
12-11 23:02:05.938: E/AndroidRuntime(4215): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.events/com.example.events.EventList}: java.lang.NullPointerException
12-11 23:02:05.938: E/AndroidRuntime(4215):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-11 23:02:05.938: E/AndroidRuntime(4215):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-11 23:02:05.938: E/AndroidRuntime(4215):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-11 23:02:05.938: E/AndroidRuntime(4215):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-11 23:02:05.938: E/AndroidRuntime(4215):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 23:02:05.938: E/AndroidRuntime(4215):     at android.os.Looper.loop(Looper.java:137)
12-11 23:02:05.938: E/AndroidRuntime(4215):     at android.app.ActivityThread.main(ActivityThread.java:5103)
12-11 23:02:05.938: E/AndroidRuntime(4215):     at java.lang.reflect.Method.invokeNative(Native Method)
12-11 23:02:05.938: E/AndroidRuntime(4215):     at java.lang.reflect.Method.invoke(Method.java:525)
12-11 23:02:05.938: E/AndroidRuntime(4215):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-11 23:02:05.938: E/AndroidRuntime(4215):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-11 23:02:05.938: E/AndroidRuntime(4215):     at dalvik.system.NativeStart.main(Native Method)
12-11 23:02:05.938: E/AndroidRuntime(4215): Caused by: java.lang.NullPointerException
12-11 23:02:05.938: E/AndroidRuntime(4215):     at com.example.events.EventList.element_confi(EventList.java:80)
12-11 23:02:05.938: E/AndroidRuntime(4215):     at com.example.events.EventList.onCreate(EventList.java:63)
12-11 23:02:05.938: E/AndroidRuntime(4215):     at android.app.Activity.performCreate(Activity.java:5133)
12-11 23:02:05.938: E/AndroidRuntime(4215):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-11 23:02:05.938: E/AndroidRuntime(4215):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-11 23:02:05.938: E/AndroidRuntime(4215):     ... 11 more

And, just in case, the layouts as well:

activity_event_list

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".EventList" >

<LinearLayout  
android:id="@+id/linear"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical" 
     android:gravity="center">

 <ListView
     android:id="@+id/list"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical" 
     android:gravity="center"

     >
      </ListView>   

 </LinearLayout>

<TextView
    android:id="@+id/nom"
    android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nom Event" 
        android:layout_below="@+id/linear"
    />

<android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/view_pager"
        android:layout_below="@id/nom"
        android:layout_width="match_parent"
        android:layout_height="200dp" 
        />

<TextView
        android:id="@+id/data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/view_pager"
        android:text="TextView" />

<Button
        android:id="@+id/mapa"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/data"
        android:text="Com arribar" />

     <Button
        android:id="@+id/join"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/mapa"
        android:text="Join event" />


     <TextView
        android:id="@+id/descripcio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/join"
        android:text="Descripció event" />  

 </RelativeLayout>

And a custom layout too:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="65dp"
        android:layout_height="65dp" />

    <ImageView
        android:id="@+id/img_boomark"
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:src="@drawable/favorito" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/nom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/place"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="text view" />

        <TextView
            android:id="@+id/data"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </LinearLayout>

</LinearLayout>

I added a comment next to the line where my mistake is at, according to the LogCat.

Could someone please point out where I am mistaken? I would be very grateful if you did so.

Yours sincerely,

Mauro.

Upvotes: 0

Views: 69

Answers (1)

Shayan Pourvatan
Shayan Pourvatan

Reputation: 11948

you got NPE on line 80:

bookmark.setImageResource(R.drawable.favorito1);//line 80!

so bookmark is null:

you write:

bookmark = (ImageView) findViewById(R.id.bookmark);

but you don't have bookmark id on activity_event_list

Upvotes: 1

Related Questions