Android:从broadcastReceiver更新listView中的进度条

时间:2014-05-12 10:29:53

标签: android listview broadcastreceiver progress-bar wifi

我使用broadcastReceiver接收wifi信号强度,并且我试图在progressBar中显示wifi级别。但是我得到了一个例外。

private class MonAdaptateurDeListe extends ArrayAdapter<ScanResult> {

    public MonAdaptateurDeListe(){
        super(MainActivity.this,R.layout.item_layout,results);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View itemView = convertView;
        if (itemView == null){
            itemView = getLayoutInflater().inflate(R.layout.item_layout, parent, false);
        }

        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        ImageView imageView = (ImageView)itemView.findViewById(R.id.imageView1);
        imageView.setImageResource(R.drawable.network);
                Log.i("position",position+"");
                Log.i("position",results+"");
                if (results != null)
                { 
                    progressBar.setMax(-100);
                    progressBar.setMax(-20);
                    progressBar.setProgress(results.get(position).level);

        }

        return itemView;
    }

}

这是我的XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/security"
        android:layout_alignParentRight="true"
        android:layout_marginRight="16dp"
        android:minWidth="70dp"
        android:progressDrawable="@drawable/greenprogress" />

这就是我得到的LogCat,它指向&#34; progressBar.setMax(-100);&#34; line:

    05-12 11:24:59.156: E/AndroidRuntime(5375): FATAL EXCEPTION: main
05-12 11:24:59.156: E/AndroidRuntime(5375): java.lang.NullPointerException
05-12 11:24:59.156: E/AndroidRuntime(5375):     at com.example.listwifi.MainActivity$MonAdaptateurDeListe.getView(MainActivity.java:146)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.widget.AbsListView.obtainView(AbsListView.java:2207)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.widget.ListView.onMeasure(ListView.java:1162)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.view.View.measure(View.java:15635)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.view.View.measure(View.java:15635)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.view.View.measure(View.java:15635)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:850)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.view.View.measure(View.java:15635)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2200)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.view.View.measure(View.java:15635)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2165)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1249)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1443)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4872)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.view.Choreographer.doCallbacks(Choreographer.java:579)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.view.Choreographer.doFrame(Choreographer.java:548)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.os.Handler.handleCallback(Handler.java:800)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.os.Handler.dispatchMessage(Handler.java:100)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.os.Looper.loop(Looper.java:194)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at android.app.ActivityThread.main(ActivityThread.java:5371)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at java.lang.reflect.Method.invokeNative(Native Method)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at java.lang.reflect.Method.invoke(Method.java:525)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-12 11:24:59.156: E/AndroidRuntime(5375):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

您是否将进度条设置为 -100 ?那不是你的错吗?我非常确定进度条的最低允许值是...... 0.请参阅the ProgressBar API documentation,您甚至无法为进度条设置最小值(但您可以设置最大值) )。

答案 1 :(得分:0)

我找到了一个解决方案。希望它会帮助别人。

      private Handler mHandler = new Handler();
     private ProgressBar progressBar;
    if (results != null)
                        { 
                            pos = position;

                            // Start lengthy operation in a background thread
                             new Thread(new Runnable() {
                                 public void run() {

                                         // Update the progress bar
                                         mHandler.post(new Runnable() {
                                             public void run() {
                                            progressBar.setProgress(30);
}
                     });
                 }
             }
         }).start();