gpt4 book ai didi

安卓工作室 "Attempt to invoke virtual method on a null object reference"

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:58:54 24 4
gpt4 key购买 nike

当我创建 ListView 的每个项目的自定义 View 时,我得到一个空指针异常,我不知道为什么,布局 id 似乎是正确的

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

主要 Activity 布局:

<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" />

项目 View 布局:

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

欢迎任何帮助和解释

最佳答案

在你的代码中

itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);

getLayoutInflater() 方法在内部适配器类中不可用。因此,您应该尝试创建一个变量以拥有父类的 Layout inflater 对象并访问它,或者您可以在适配器类的构造函数中发送调用类的上下文。

因此你可以像这样使用:

public class ActivityMainWish extends Activity {

LayoutInflate inflater;
private List<Wish> myWishs = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
inflater=getLayoutInflater()
...
}

并在此处使用此 inflater 变量: itemView = inflater.inflate(R.layout.item_view, parent, false);

或者像这样:

private class MyListAdapter extends ArrayAdapter<Wish>{
private Context mContext;
public MyListAdapter(Context ctx){
super(ActivityMainWish.this, R.layout.item_view, myWishs);
mContext=ctx;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (itemView==null)
itemView = LayoutInflater.from(mContext).inflate(R.layout.item_view, parent, false);

并像这样发送 Context 对象:

ArrayAdapter<Wish> adapter = new MyListAdapter(getApplicationContext());

关于安卓工作室 "Attempt to invoke virtual method on a null object reference",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30685422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com