ProgressBar.setProgress抛出异常

时间:2014-05-15 08:33:37

标签: android exception android-progressbar

我正在尝试在进度条中显示信号强度级别,但我在该行中遇到异常:progressBar.setProgress(10); 可能是什么原因?

public class MonAdaptateurDeListe extends ArrayAdapter<ScanResult> {
    int pos ;
    TextView text;
    public MonAdaptateurDeListe(){
        super(WiFiScanResult.this,R.layout.item_layout,results);

    }


    @SuppressLint("DefaultLocale")
    @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);
        }


        ImageView imageView = (ImageView)itemView.findViewById(R.id.imageView1);

        TextView textView4 = (TextView) itemView.findViewById(R.id.level);
        imageView.setImageResource(R.drawable.network);

        if (results != null)
        { 
        textView4 .setText(results.get(position).level+"dBm");

        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        progressBar.setProgress(10);

        TextView textView1 = (TextView) itemView.findViewById(R.id.SSIDBssid);
        textView1.setText( results.get(position).SSID +"(" + results.get(position).BSSID +")");

        TextView textView3 = (TextView) itemView.findViewById(R.id.frequency);
        textView3.setText(results.get(position).frequency+"Mhz");


        TextView textView5 = (TextView) itemView.findViewById(R.id.security);
        textView5.setText(results.get(position).capabilities);

            }


        }

        return itemView;
    }

}

logCat

05-15 09:27:32.650: W/dalvikvm(12645): threadid=1: thread exiting with uncaught exception (group=0x40cb39a8)
05-15 09:27:32.670: E/AndroidRuntime(12645): FATAL EXCEPTION: main
05-15 09:27:32.670: E/AndroidRuntime(12645): java.lang.NullPointerException
05-15 09:27:32.670: E/AndroidRuntime(12645):    at com.example.debitdistance.WiFiScanResult$MonAdaptateurDeListe.getView(WiFiScanResult.java:140)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.widget.AbsListView.obtainView(AbsListView.java:2207)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.widget.ListView.onMeasure(ListView.java:1162)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.view.View.measure(View.java:15635)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.view.View.measure(View.java:15635)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.view.View.measure(View.java:15635)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:850)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.view.View.measure(View.java:15635)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2200)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.view.View.measure(View.java:15635)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2165)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1249)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1443)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4872)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.view.Choreographer.doCallbacks(Choreographer.java:579)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.view.Choreographer.doFrame(Choreographer.java:548)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.os.Handler.handleCallback(Handler.java:800)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.os.Handler.dispatchMessage(Handler.java:100)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.os.Looper.loop(Looper.java:194)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at android.app.ActivityThread.main(ActivityThread.java:5371)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at java.lang.reflect.Method.invokeNative(Native Method)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at java.lang.reflect.Method.invoke(Method.java:525)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-15 09:27:32.670: E/AndroidRuntime(12645):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:5)

您的progressBar来自itemview布局,因此您需要使用该特定布局对其进行初始化。

试试这个:

 progressBar = (ProgressBar) itemview.findViewById(R.id.progressBar1);

而不是

 progressBar = (ProgressBar) findViewById(R.id.progressBar1);