Question

I'm receiving wifi signal strength with the broadcastReceiver ,and i'm trying to diplay the wifi level in a progressBar. But i'm getting an exception.

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;
    }

}

And this is my XML file :

<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" />

And this is the LogCat i'm getting ,and it's pointing on "progressBar.setMax(-100);" 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)
Was it helpful?

Solution

You're setting the progress bar to -100? Isn't that your error? I'm pretty sure the lowest permitted value for a progress bar is... 0. See the ProgressBar API documentation, you can't even set a minimum value for a progress bar (but you can set a maximum).

OTHER TIPS

I found a solution .Hope it will help someone.

      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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top