when i create the custom view of each items of the list view, i get a null pointer exception and i dont know why, the layout id seems correct
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;
}
}
}
main activity layout :
<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" />
the item view layout :
<?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" />
and here the error
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.gokhan.mywishlist.ActivityMainWish$MyListAdapter.getView(ActivityMainWish.java:90)
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)
any help and explanition is welcomed
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…