android启动活动错误

时间:2014-03-02 16:49:51

标签: android

我正在使用一个显示对象列表的应用程序,当你点击一个对象时,它应该切换到一个显示详细信息的屏幕:应用程序在那一刻崩溃但是错误所在的地方感觉很奇怪而且我没有发现事故原因。

这是我的活动EventList的代码,链接到名为Item Event的类:

活动清单

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


}

另外一个类,一个适配器。

项目事件适配器

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


}

这是我的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

而且,以防万一,布局也是如此:

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>

也是自定义布局:

<?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>

根据LogCat,我在错误所在的行旁边添加了评论。

有人可以指出我错在哪里吗?如果你这样做,我将非常感激。

您诚挚的,

莫罗。

1 个答案:

答案 0 :(得分:1)

你在第80行得到NPE

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

所以书签是null

你写道:

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

但您在id

上没有书签activity_event_list