gpt4 book ai didi

java - BaseAdapter 类 getView() 中的 Layoutinflater 空指针异常

转载 作者:行者123 更新时间:2023-12-02 04:34:53 24 4
gpt4 key购买 nike

我尝试了其他人的问题的解决方案,但没有成功。我在 LayoutInflater 的 BaseAdapter 类中收到以下错误:

06-21 17:24:38.808: E/AndroidRuntime(23959): FATAL EXCEPTION: main
06-21 17:24:38.808: E/AndroidRuntime(23959): Process: org.nick.yarntrading, PID: 23959
06-21 17:24:38.808: E/AndroidRuntime(23959): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.nick.yarntrading/org.nick.yarntrading.MainActivity}: java.lang.NullPointerException
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.app.ActivityThread.access$800(ActivityThread.java:151)
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.os.Handler.dispatchMessage(Handler.java:110)
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.os.Looper.loop(Looper.java:193)
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.app.ActivityThread.main(ActivityThread.java:5292)
06-21 17:24:38.808: E/AndroidRuntime(23959): at java.lang.reflect.Method.invokeNative(Native Method)
06-21 17:24:38.808: E/AndroidRuntime(23959): at java.lang.reflect.Method.invoke(Method.java:515)
06-21 17:24:38.808: E/AndroidRuntime(23959): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
06-21 17:24:38.808: E/AndroidRuntime(23959): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
06-21 17:24:38.808: E/AndroidRuntime(23959): at dalvik.system.NativeStart.main(Native Method)
06-21 17:24:38.808: E/AndroidRuntime(23959): Caused by: java.lang.NullPointerException
06-21 17:24:38.808: E/AndroidRuntime(23959): at org.nick.card_view.DataListAdapter.<init>(DataListAdapter.java:34)
06-21 17:24:38.808: E/AndroidRuntime(23959): at org.nick.yarntrading.Home.onCreateView(Home.java:36)
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1174)
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.app.Activity.performStart(Activity.java:5274)
06-21 17:24:38.808: E/AndroidRuntime(23959): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
06-21 17:24:38.808: E/AndroidRuntime(23959): ... 11 more

这是扩展 BaseAdapter 的 DataAdapter 类:

public class DataListAdapter extends BaseAdapter {

App app;
ArrayList<HashMap<String,String>> listdata = null;
FragmentManager fm;
LayoutInflater inflater = null;
LinearLayout lnlyt;


//ProductList prodctlst;
public DataListAdapter(App a, FragmentManager f)
{
this.app =a;
this.fm = f;
**inflater = (LayoutInflater) app.getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);**
listdata = new ProductList(this.app).returnList();
}
@Override
public int getCount() {

return listdata.size();
}

@Override
public Object getItem(int position) {
return listdata.get(position);
}

@Override
public long getItemId(int position) {
return 0;
}

public class ViewHolder{
TextView prod_name,prod_price,prod_descrp;

}
@Override
public View getView(int position, View v, ViewGroup parent) {
final ViewHolder h;
if(v==null)
{

v = inflater.inflate(R.layout.card_view,null);
h = new ViewHolder();
h.prod_name =(TextView) lnlyt.findViewById(R.id.prod_name);
h.prod_price = (TextView)lnlyt.findViewById(R.id.prod_price);
h.prod_descrp = (TextView) lnlyt.findViewById(R.id.prod_description);
v.setTag(h);
}else
h = (ViewHolder) v.getTag();
h.prod_name.setText(listdata.get(position).get("prodctName"));
h.prod_price.setText(listdata.get(position).get("prodctPrice"));
h.prod_descrp.setText(listdata.get(position).get("prodctDescription"));

return v;
}

}

这是 xml 文件:

<?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">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/feed_item_margin"
android:layout_marginRight="@dimen/feed_item_margin"
android:layout_marginTop="@dimen/feed_item_margin"
android:background="@drawable/bg_parent_rounded_corner"
android:orientation="vertical"
android:paddingTop="@dimen/feed_item_padding_top_bottom" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="@dimen/feed_item_padding_left_right"
android:paddingRight="@dimen/feed_item_padding_left_right" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/feed_item_profile_info_padd" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/prod_name"
android:textStyle="bold"
android:text="Mobile"
android:textSize="20sp"/>

<TextView
android:id="@+id/prod_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="140dp"
android:layout_toEndOf="@+id/prod_name"
android:text="15.2641"
android:textSize="20sp" />

</RelativeLayout>

</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:layout_marginTop="8dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/prod_description"
android:textColor="@android:color/black"
android:textSize="18sp"
android:text="Description"
/>

</LinearLayout>
</LinearLayout>

这是Home类的代码:

package org.nick.yarntrading;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;

import org.nick.card_view.DataListAdapter;


public class Home extends ListFragment {
App a;
ListView card_list;
DataListAdapter adapter;
RelativeLayout prodctView;
public void home(App a)
{
this.a = a;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}
@SuppressLint("InflateParams") @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
prodctView = (RelativeLayout)inflater.inflate(R.layout.home_listview,null);
card_list = (ListView)prodctView.findViewById(android.R.id.list);
adapter = new DataListAdapter(a,getActivity().getSupportFragmentManager(),getActivity());
card_list.setAdapter(adapter);
return prodctView;
}

}

App 类扩展了应用程序。我尝试了 stackoverflow 上提供的其他解决方案,但没有成功。这里我想做的是登录应用程序后显示列表\ View 。

任何想法都会受到赞赏。提前致谢。

最佳答案

嘿,

    v = inflater.inflate(R.layout.card_view,null);
h = new ViewHolder();
h.prod_name =(TextView) *lnlyt*.findViewById(R.id.prod_name);
v.setTag(h);
}else
h = (ViewHolder) v.getTag();
h.prod_name.setText(listdata.get(position).get("prodctName"));

return v;
}

您正在通过 LynLyt 引用您的 View ,但您甚至还没有初始化它,请尝试以下操作:

@Override
public View getView(int position, View v, ViewGroup parent) {
//final ViewHolder h;// you have just declared it , initialize it first
final ViewHolder h = new ViewHolder();
if(v==null)
{

v = inflater.inflate(R.layout.card_view,null);
h = new ViewHolder();
h.prod_name =(TextView) v.findViewById(R.id.prod_name);
h.prod_price = (TextView)v.findViewById(R.id.prod_price);
h.prod_descrp = (TextView) v.findViewById(R.id.prod_description);
v.setTag(h);
}else
h = (ViewHolder) v.getTag();
h.prod_name.setText(listdata.get(position).get("prodctName"));
h.prod_price.setText(listdata.get(position).get("prodctPrice"));
h.prod_descrp.setText(listdata.get(position).get("prodctDescription"));

return v;
}

逻辑是这样的,一旦你像这样膨胀你的布局,

v = inflater.inflate(R.layout.card_view,null); 现在您已将布局包含在 View “V”中,现在可以查找引用“v”的 View 。 !

希望对你有帮助。

关于java - BaseAdapter 类 getView() 中的 Layoutinflater 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30964337/

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