我编写Android应用程序时有一个NPE。我已经阅读了至少数百个关于它的主题而没有找到任何答案。所以我终于直接在那里问了。这是错误:
01-19 21:13:09.886: E/AndroidRuntime(6622): FATAL EXCEPTION: main
01-19 21:13:09.886: E/AndroidRuntime(6622): java.lang.NullPointerException
01-19 21:13:09.886: E/AndroidRuntime(6622): at esstin.projet.ExpositionAdaptateur.getView(ExpositionAdaptateur.java:59)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.widget.AbsListView.obtainView(AbsListView.java:2465)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.widget.ListView.onMeasure(ListView.java:1162)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.view.View.measure(View.java:15504)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.view.View.measure(View.java:15504)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.view.View.measure(View.java:15504)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.view.View.measure(View.java:15504)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-19 21:13:09.886: E/AndroidRuntime(6622): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2385)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.view.View.measure(View.java:15504)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1979)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1222)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1395)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4526)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.view.Choreographer.doFrame(Choreographer.java:525)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.os.Handler.handleCallback(Handler.java:615)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.os.Handler.dispatchMessage(Handler.java:92)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.os.Looper.loop(Looper.java:137)
01-19 21:13:09.886: E/AndroidRuntime(6622): at android.app.ActivityThread.main(ActivityThread.java:4921)
01-19 21:13:09.886: E/AndroidRuntime(6622): at java.lang.reflect.Method.invokeNative(Native Method)
01-19 21:13:09.886: E/AndroidRuntime(6622): at java.lang.reflect.Method.invoke(Method.java:511)
01-19 21:13:09.886: E/AndroidRuntime(6622): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
01-19 21:13:09.886: E/AndroidRuntime(6622): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
01-19 21:13:09.886: E/AndroidRuntime(6622): at dalvik.system.NativeStart.main(Native Method)
然后是“ExpositionAdaptateur”类,它是自定义适配器(带有“nomExpo.setText(listeExpo.get(position).getNom())”的行中的错误;“:
package esstin.projet;
import java.util.List;
import esstin.projet.accesbase.Exposition;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ExpositionAdaptateur extends BaseAdapter {
List<Exposition> listeExpo;
LayoutInflater inflater;
Context context;
public ExpositionAdaptateur(Context context, List<Exposition> listeExpo) {
//inflater = LayoutInflater.from(context);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.listeExpo = listeExpo;
this.context = context;
}
@Override
public int getCount() {
return listeExpo.size();
}
@Override
public Object getItem(int position) {
return listeExpo.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout layoutItem;
//(1) : Réutilisation des layouts
if (convertView == null) {
//Initialisation de notre item à partir du layout XML "personne_layout.xml"
layoutItem = (LinearLayout) inflater.inflate(R.layout.expositionadaptateur, parent, false);
} else {
layoutItem = (LinearLayout) convertView;
}
//(2) : Récupération des TextView de notre layout
TextView nomExpo = (TextView)layoutItem.findViewById(R.id.nomExpo);
//(3) : Renseignement des valeurs
nomExpo.setText(listeExpo.get(position).toString());
//On retourne l'item créé.
return layoutItem;
}
}
这是我的ListView的每一行的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/nomExpo"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TextView>
</LinearLayout>
然后是主要布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listViewExpo"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
最后是主要课程:
package esstin.projet;
import java.util.ArrayList;
import java.util.List;
import esstin.projet.accesbase.Exposition;
import esstin.projet.accesbase.ExpositionBDD;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class AccueilGalerie_activity extends Activity implements View.OnClickListener {
private Button boutonExpo1 = null;
private Button boutonExpo2 = null;
private Button boutonExpo3 = null;
private TextView expo1TxtView = null;
List<Exposition> listeExposition = new ArrayList<Exposition>();
ListView listViewExpo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.accueilgalerie_activity);
int count = 0;
ExpositionBDD expoBdd = new ExpositionBDD(this);
expoBdd.open();
while(count <= expoBdd.getNumberOfRows()){
Exposition expoFromDb = expoBdd.getExpositionWithId(count);
listeExposition.add(expoFromDb);
count++;
}
expoBdd.close();
ExpositionAdaptateur adapter = new ExpositionAdaptateur(this,listeExposition);
listViewExpo = (ListView)findViewById(R.id.listViewExpo);
listViewExpo.setAdapter(adapter);
}
@Override
public void onClick(View v) {
}
}
我试图在一周后修复它,所以如果有人能找到正在发生的事情那将会很棒:)!
谢谢!
答案 0 :(得分:0)
int count = 0;
//...
while(count <= expoBdd.getNumberOfRows()){
Exposition expoFromDb = expoBdd.getExpositionWithId(count);
listeExposition.add(expoFromDb);
count++;
}
此循环运行getNumberOfRows() + 1
次。现在,根据getExpositionWithId()
实施,可能会导致null
在列表中结束,在此处调用toString()
时会导致NPE:
nomExpo.setText(listeExpo.get(position).toString());
快速解决方法:将<=
更改为<
。