Android中的致命异常错误

时间:2014-01-04 13:21:07

标签: android fatal-error

任何人都可以告诉我这个logcat要求的是什么吗?

建议请。

感谢您宝贵的时间!..

Adapter_available.java

public View getView(int position, View convertView, ViewGroup parent) 
{
    ViewHolder holder = null;

    if (convertView == null) 
    {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.child_available_layout, null);

        holder = new ViewHolder();
        holder.txt_plname    = (TextView) convertView.findViewById(R.id.textView_planname);
        holder.txt_price     = (TextView) convertView.findViewById(R.id.textView_price);
        holder.txt_size      = (TextView) convertView.findViewById(R.id.textView_size);
        holder.btn_buy_ = (Button) convertView.findViewById(R.id.button_buy);

//          convertView.setTag(holder);
        holder = (ViewHolder) convertView.getTag();


    Available_contents contents =  originalist.get(position);

    holder.txt_price.setText("K "+contents.price);
    holder.txt_size.setText(contents.size);
    holder.txt_plname.setText(contents.planname);

    }
return convertView;
}

logcat的

01-04 18:29:04.705: E/AndroidRuntime(12701): FATAL EXCEPTION: main
01-04 18:29:04.705: E/AndroidRuntime(12701): java.lang.NullPointerException
01-04 18:29:04.705: E/AndroidRuntime(12701):    at org.twinkle.bemobile.Adapter_available.getView(Adapter_available.java:57)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.widget.AbsListView.obtainView(AbsListView.java:2472)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.widget.ListView.onMeasure(ListView.java:1162)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.View.measure(View.java:15524)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.View.measure(View.java:15524)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.View.measure(View.java:15524)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.View.measure(View.java:15524)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2397)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.View.measure(View.java:15524)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.View.measure(View.java:15524)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.View.measure(View.java:15524)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.View.measure(View.java:15524)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.View.measure(View.java:15524)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at   android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.View.measure(View.java:15524)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.View.measure(View.java:15524)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2397)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.View.measure(View.java:15524)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1986)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1227)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at   android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1400)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1120)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4604)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.Choreographer.doFrame(Choreographer.java:525)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.os.Handler.handleCallback(Handler.java:615)
01-04 18:29:04.705: E/AndroidRuntime(12701):    at android.os.Handler.dispatch

2 个答案:

答案 0 :(得分:1)

更改getView

您使用ViewHolder模式。您在convertView为空时初始化视图。您将代码设置为convertView

convertView.setTag(holder); 

如果convertView不为空

holder = (ViewHolder) convertView.getTag();

正如西蒙在评论中提到的那样

//  convertView.setTag(holder); // commented
    holder = (ViewHolder) convertView.getTag(); // using get tag
    // this line probably gives you NPE

How ListView's recycling mechanism works

上述链接可能更好地解释了列表回收的工作原理

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

使用ViewHolder模式有助于平滑滚动和提高性能。

public View getView(int position, View convertView, ViewGroup parent) 
{
    ViewHolder holder = null;

    if (convertView == null) 
    {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.child_available_layout, parent,false);

        holder = new ViewHolder();
        holder.txt_plname    = (TextView) convertView.findViewById(R.id.textView_planname);
        holder.txt_price     = (TextView) convertView.findViewById(R.id.textView_price);
        holder.txt_size      = (TextView) convertView.findViewById(R.id.textView_size);
        holder.btn_buy_ = (Button) convertView.findViewById(R.id.button_buy);

        convertView.setTag(holder); 
        // this was commented. no need to do that
       } 
       else
       { 
        holder = (ViewHolder) convertView.getTag(); 
        // if convertVIew is not null initialize the holder using convertView.getTag

       }  


    Available_contents contents =  originalist.get(position);

    holder.txt_price.setText("K "+contents.price);
    // set text to text view and update it here 
    holder.txt_size.setText(contents.size);
    holder.txt_plname.setText(contents.planname);

    }
return convertView;
}

答案 1 :(得分:1)

您不在代码中使用SetTag(),但是您需要使用以下代码:

holder = (ViewHolder) convertView.getTag();

你必须首先setTag()然后得到它,你需要else语句来检查获取Tag。

if (convertView == null) 
    {

        holder = new ViewHolder();

        // define parameter
        convertView.setTag(holder); 

       } 
       else
       { 
        holder = (ViewHolder) convertView.getTag();
       }  
       // setText and other thing
  return convertView;