当我创建列表视图的每个项目的自定义视图时,我会在列表视图上添加的当前愿望上获得空指针异常。
编辑:我的代码工作但它序列化了textviews内容和活动之间的愿望图片ID,但我决定在意图中使用Wish对象,现在我得到这个空指针错误:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class ActivityMainWish extends Activity {
private List<Wish> myWishs = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_wish);
creerListe();
creerListeView();
afficherWishView();
ajouterWishListe();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_activity_main_wish, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
//fct
return true;
}
if (id ==R.id.action_add_wish){
startActivity(new Intent(this,ActivityAddWish.class));
return true;
}
return super.onOptionsItemSelected(item);
}
public void creerListe() {
myWishs.add(new Wish("Voiture", 50000, getResources().getDrawable(R.drawable.voiture,null)));
myWishs.add(new Wish("TV", 5000, getResources().getDrawable(R.drawable.tv,null)));
myWishs.add(new Wish("Smartphone", 500, getResources().getDrawable(R.drawable.smarphone, null)));
}
public void creerListeView (){
//convertire les items en views appropier
//prend cette liste, le Layout utilise (creer), et la liste d'items a afficher
ArrayAdapter<Wish> adapter = new MyListAdapter();
//prend la liste
ListView list = (ListView) findViewById(R.id.ListViewWishList);
//configure la liste
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<Wish>{
public MyListAdapter(){
super(ActivityMainWish.this, R.layout.item_view, myWishs);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (itemView==null)
itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
Wish currentWish = myWishs.get(position);
TextView textView = (TextView) itemView.findViewById(R.id.TextViewWishListNom);
textView.setText((currentWish.getNom()));
TextView prixView = (TextView) itemView.findViewById(R.id.TextViewWishListPrix);
prixView.setText(("" + currentWish.getPrix()));
ImageView imageView = (ImageView) itemView.findViewById(R.id.ImageViewWishListImage);
imageView.setImageDrawable(currentWish.getImage());
return itemView;
}
}
}
希望上课:
package com.gokhan.mywishlist;
import android.graphics.drawable.Drawable;
/**
* Created by Gokhan on 29/05/2015.
*/
public class Wish {
private String nom;
private float prix;
private Drawable image;
public Wish(String nom, float prix, Drawable image){
super();
this.nom = nom;
this.prix = prix;
this.image = image;
}
public Wish(){
}
public String getNom (){
return nom;
}
public float getPrix(){
return prix;
}
public Drawable getImage(){
return image;
}
public void setNom(String nom){
this.nom = nom;
}
public void setPrix(float prix){
this.prix = prix;
}
public void setImage(Drawable image){
this.image = image;
}
}
主要活动布局:
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".ActivityMainWish">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ListViewWishList"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
项目视图布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/wishList">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ImageViewWishListImage"
android:src="@drawable/voiture"
android:minWidth="80dp"
android:minHeight="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:maxHeight="80dp"
android:maxWidth="80dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Voiture"
android:id="@+id/TextViewWishListNom"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="50000"
android:id="@+id/TextViewWishListPrix"
android:layout_alignBottom="@+id/ImageViewWishListImage"
android:layout_alignParentEnd="true" />
这里是错误
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.gokhan.mywishlist.Wish.getNom()' on a null object reference
at com.gokhan.mywishlist.ActivityMainWish$CustomArrayAdapter.getView(ActivityMainWish.java:94)
at android.widget.AbsListView.obtainView(AbsListView.java:2347)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
at android.widget.ListView.onMeasure(ListView.java:1182)
at android.view.View.measure(View.java:17547)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:447)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
at android.view.View.measure(View.java:17547)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
欢迎任何帮助和解释。
答案 0 :(得分:0)
发现错误!我在函数“ajouterWishListe()”中添加了这个,它将一个项添加到列表中,即使intent是空的,它也添加了一个null对象,所以当我调用getNom()时,Wish为null
if ((Wish)intent.getSerializableExtra("ADDEDWISH")!= null){