2014-03-02 190 views
0

我正在一個顯示對象列表的應用程序中工作,當你點擊一個應用程序時,它應該切換到一個顯示細節的屏幕:應用程序在那一刻崩潰了,但是出現錯誤的位置感到很奇怪,我不明白這次事故的原因。android開始活動錯誤

下面是我的活動EVENTLIST代碼,聯繫到一類名爲項事件:

事件列表

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

類項事件

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

你就行了80 NPE

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

所以書籤是null

你寫:

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

,但你沒有書籤idactivity_event_list

+0

哦!確實是這樣!非常感謝:我感到非常愚蠢,忽略了這樣一個基本的東西......謝謝。 – user3082271

+0

歡迎,很高興幫助 –