2012-03-03 185 views
0

我觉得这种问题有点愚蠢,因为我有另一个应用程序,它可以正常工作,或者至少我认为它是相同的。我对此肯定需要另一套眼睛,因为对我来说,它看起来应该起作用。我的代码如下:Android Handler无法访问图形用户界面元素

public class InventorySystemActivity extends Activity { 
private EditText barcode; 
private EditText proddesc; 
private EditText quantity; 
public String scan_result; 
public int which; 
private InventorySystemDB db; 
public Item singleItem; 
public Item[] itemList; 
private Thread t; 
Handler handler = new Handler() { 
    public void handleMessage(Message msg) { 
     switch (msg.what) { 
     case 1: 
      barcode.setText(scan_result); 
      proddesc.setText(singleItem.name); 
      quantity.setText(singleItem.quantity); 
      break; 
     case 3: 
      updateUI(); 
      pd.dismiss(); 
     } 
    } 
}; 
public static Intent in = new Intent("com.google.zxing.client.android.SCAN"); 
private ProgressDialog pd; 
private Thread dbt; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.db = new InventorySystemDB(this); 
    try { 
     this.db.createDataBase(); 
     this.db.openDataBase(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    this.dbt = new Thread(this.db); 
    setContentView(R.layout.main); 
    Button scan = (Button) findViewById(R.id.scan); 
    scan.setOnClickListener(new Button.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      startActivityForResult(in, 0); 
     } 
    }); 
    Button search = (Button) findViewById(R.id.searchbutton); 
    search.setOnClickListener(new Button.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      pd.show(); 
      which = 1; 
      scan_result = barcode.getText().toString(); 
      dbt.start(); 
     } 
    }); 
    barcode = (EditText) findViewById(R.id.barcodenum); 
    proddesc = (EditText) findViewById(R.id.proddesc); 
    quantity = (EditText) findViewById(R.id.prodquantity); 
    pd = new ProgressDialog(this); 
    pd.setMessage("Loading data..."); 
} 
public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
    if (requestCode == 0) { 
     if (resultCode == RESULT_OK) { 
      scan_result = intent.getStringExtra("SCAN_RESULT"); 
      barcode.setText(scan_result); 
     } else if (resultCode == RESULT_CANCELED) { 

     } 
    } 
} 
private void updateUI() { 
    barcode.setText(singleItem.upc); 
    proddesc.setText(singleItem.name); 
    quantity.setText("0"); 
} 

}

我所有的线程做是需要的UPC号码,并从一对夫妇的网站拉信息。代码发送消息到处理程序很好,我已经使用线程和处理程序创建了一些类似的应用程序,但不知道为什么现在有这个问题。无论如何,我可以关闭ProgressDialog,但我无法更新任何UI对象。这一切对我来说都很好,所以我真的需要更多的眼光。多谢你们。

回答