使用bsAdapter.getView()时获取NullPointerException

时间:2013-12-03 12:33:16

标签: android android-listview android-adapter

public View getView( final int position, View convertView, ViewGroup parent)
        {

            LayoutInflater inflater=cntx.getLayoutInflater();
            View row = inflater.inflate(R.layout.simplerow, null);
          //  row=inflater.inflate(R.layout.simplerow, parent, false);

            DecimalFormat df = new DecimalFormat("#.##");
            final TextView title    =   (TextView)row.findViewById(android.R.id.title);
             TextView tvcol1    =   (TextView)row.findViewById(R.id.colortext01);


            TextView gt     =   (TextView)row.findViewById(R.id.text01);


           tvcol1.setHeight(grossheight[position]);

           title.setText(datelabel[position]);


           gt.setText(df.format(grossSal[position])+" /-");

 return row;
        }

的logcat @

java.lang.NullPointerException
       at com.pawan.my2dchart1.MyChart$bsAdapter.getView(MyChart.java:109)
       at com.pawan.my2dchart1.HorizontalListView.fillListRight(HorizontalListView.java:239)
       at com.pawan.my2dchart1.HorizontalListView.fillList(HorizontalListView.java:224)
       at com.pawan.my2dchart1.HorizontalListView.onLayout(HorizontalListView.java:201)
       at android.view.View.layout(View.java:14289)
       at android.view.ViewGroup.layout(ViewGroup.java:4562)
       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
       at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1660)
       at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
       at android.view.View.layout(View.java:14289)
       at android.view.ViewGroup.layout(ViewGroup.java:4562)
       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
       at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
       at android.view.View.layout(View.java:14289)
       at android.view.ViewGroup.layout(ViewGroup.java:4562)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
       at android.widget.ScrollView.onLayout(ScrollView.java:1459)
       at android.view.View.layout(View.java:14289)
       at android.view.ViewGroup.layout(ViewGroup.java:4562)
       at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
       at android.view.View.layout(View.java:14289)
       at android.view.ViewGroup.layout(ViewGroup.java:4562)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
       at android.view.View.layout(View.java:14289)
       at android.view.ViewGroup.layout(ViewGroup.java:4562)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
       at android.view.View.layout(View.java:14289)
       at android.view.ViewGroup.layout(ViewGroup.java:4562)
       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
       at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
       at android.view.View.layout(View.java:14289)
       at android.view.ViewGroup.layout(ViewGroup.java:4562)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
       at android.view.View.layout(View.java:14289)
       at android.view.ViewGroup.layout(ViewGroup.java:4562)
       at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
       at android.view.Choreographer.doCallbacks(Choreographer.java:562)
       at android.view.Choreographer.doFrame(Choreographer.java:532)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
       at android.os.Handler.handleCallback(Handler.java:730)
       at android.os.Handler.dispatchMessage(Handler.java:92)
       at android.os.Looper.loop(Looper.java:137)
       at android.app.ActivityThread.main(ActivityThread.java:5103)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:525)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
       at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

试试这段代码

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if (convertView == null) {
            View listView = new View(c);
            LayoutInflater inflater;
            listView = inflater.inflate(R.layout.simplerow,null);
        } else
            listView = (View) convertView;

         DecimalFormat df = new DecimalFormat("#.##");
         final TextView title    =   (TextView)row.findViewById(R.id.title);
        TextView tvcol1    =   (TextView)row.findViewById(R.id.colortext01);
        TextView gt     =   (TextView)row.findViewById(R.id.text01);
        tvcol1.setHeight(grossheight[position]);
        title.setText(datelabel[position]);
        gt.setText(df.format(grossSal[position])+" /-");
        return listView;
    }

答案 1 :(得分:0)

由于您(在问题的评论中)说NullPointerException出现在title.setText(datelabel[position]);行,因此有两种可能导致该错误的来源。

可能的原因1:名为TextView的{​​{1}}为title。只有在您填充错误的布局(意味着布局不包含null且标识为TextView)时才会出现这种情况。因此,解决方案是检查R.id.title是否包含ID为R.layout.simplerow的{​​{1}}。

可能原因2: TextView数组可能为R.id.title,因此请务必正确启动。

请注意,如果您在自定义datelabel中使用View Holder模式,那将是一次巨大的性能升级。有关详细信息,请参阅here