Android:ListView适配器中的Null Point异常

时间:2014-12-04 07:32:29

标签: android listview

我正在尝试开发购物车。我想在SQL4数据库的Custom ListView中显示产品详细信息,点击按钮。但是我在Adapter.My 错误日志给出了Adapter.java 。请帮助我。

错误记录

12-04 11:26:30.856: E/AndroidRuntime(24522): FATAL EXCEPTION: main
12-04 11:26:30.856: E/AndroidRuntime(24522): java.lang.NullPointerException
12-04 11:26:30.856: E/AndroidRuntime(24522):    at android.widget.AbsListView.obtainView(AbsListView.java:1317)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at android.widget.ListView.onMeasure(ListView.java:1109)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at android.view.View.measure(View.java:8171)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at android.view.View.measure(View.java:8171)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at android.view.View.measure(View.java:8171)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at android.view.View.measure(View.java:8171)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at android.view.View.measure(View.java:8171)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at android.os.Looper.loop(Looper.java:123)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at android.app.ActivityThread.main(ActivityThread.java:4627)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at java.lang.reflect.Method.invokeNative(Native Method)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at java.lang.reflect.Method.invoke(Method.java:521)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-04 11:26:30.856: E/AndroidRuntime(24522):    at dalvik.system.NativeStart.main(Native Method)
12-04 11:26:33.519: I/Process(24522): Sending signal. PID: 24522 SIG: 9

CartAdapter.java

public class CartAdapter extends BaseAdapter {
private final String[] pname;
private final String[] price;
private Context cntxt;

public CartAdapter(Context c,String [] pname,String [] price) {
   cntxt=c;
   this.pname=pname;
   this.price=price;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return pname.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View List;
    LayoutInflater mLayoutInflater=(LayoutInflater)cntxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView==null) {
        List=new View(cntxt);
        List=mLayoutInflater.inflate(R.layout.add2crt_sub,parent, false);
    } else {
        List=(View)convertView;
    }
    // ImageView imageview=(ImageView)List.findViewById(R.id.prdt_pic);
    TextView nametxt=(TextView)List.findViewById(R.id.prdt_nme);
    TextView pricetxt=(TextView)List.findViewById(R.id.prdt_rate);
    nametxt.setText(pname[position].toString());
    pricetxt.setText("Rs "+price[position] +" /-");

    return null;
}

}

1 个答案:

答案 0 :(得分:5)

您正在返回null视图。

替换getView()方法

 return null;

 return List;

并删除此行

List=new View(cntxt);