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

Java BaseAdapter类getView()中的Layoutinflater空指针异常,java,android,layout-inflater,Java,Android,Layout Inflater,我试着从别人的问题中寻找答案,但没有成功。 我在LayoutFlater的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(2

我试着从别人的问题中寻找答案,但没有成功。 我在LayoutFlater的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
}

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

任何想法都值得赞赏。 提前谢谢

Hye在那里

    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引用您的视图,但您甚至还没有初始化它,请尝试以下操作:

@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=充气机充气(右布局卡视图,空)现在您已经在视图“V”中包含了包含元素的布局,现在可以找到引用为“V”的视图了


希望我能帮助你。

这是对我有效的解决方案 我将getActivity()传递给DataListAdapter,并将其用作上下文

Context cont;
//ProductList prodctlst;
public DataListAdapter(App a, FragmentManager f,Context context)

{
    this.app =a;
    this.fm = f;
    this.cont = context;
    inflater = ((Activity) cont).getLayoutInflater();
    listdata = new ProductList(this.app).returnList();
}

在哪里调用
适配器
?我从扩展listFragment的home类调用了适配器。那么为什么不使用
getActivity()
作为
上下文
并将其传递给
适配器
您能演示如何做吗…?永远不要从
应用程序
或基本上下文获取布局充气器!始终通过
LayoutInflater.from(parentView.getContext()
)从最近的父视图获取布局充气器。如果要将适配器与
ListView
一起使用,请使用该列表视图的上下文。这将确保在不同的活动和
android:theme
属性中正确处理不同的主题(一个活动中的不同主题)并避免令人不快的惊喜(如对话框中错误的文本颜色等)。第34行标记为**,并且也发布了Home类。请检查并让我知道…我的错误我的答案没有什么变化,尝试一下,并请告诉我行:DataAdapter 34和post Home活动代码
    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;
}
@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;
}
Context cont;
//ProductList prodctlst;
public DataListAdapter(App a, FragmentManager f,Context context)

{
    this.app =a;
    this.fm = f;
    this.cont = context;
    inflater = ((Activity) cont).getLayoutInflater();
    listdata = new ProductList(this.app).returnList();
}