2012-03-26 148 views
0

我正試圖將ZXing條碼掃描器應用到我的程序中。得到掃描結果後,我想將結果解析爲屬於另一個Java類的名爲getData()的方法。 IDE上沒有語法錯誤,但mysql.getData(contents)無論如何都不會調用該方法。請指教。無法調用方法

如果我把這個代碼onCreate下,整個程序強制關閉:

package com.posQR.ip; 

import com.posQR.ip.MySQL; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

public class PosQRActivity extends Activity{ 
MySQL mysql; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);  
    mysql.getData("productID"); 
    Button button = (Button)findViewById(R.id.button1); 
    button.setOnClickListener(scanListener); 

} 

public void onActivityResult(int requestCode, int resultCode, Intent intent) { 

    TextView textView = (TextView)findViewById(R.id.textView1); 
    if (requestCode == 0) { 
    if (resultCode == RESULT_OK) { 
    try { 
     String contents = intent.getStringExtra("SCAN_RESULT"); 
     Toast.makeText(getApplicationContext(), contents + "from main", Toast.LENGTH_LONG).show(); 
     mysql.getData(contents); 
     String string = Double.toString(mysql.getPrice()); 
     textView.setText(string); 
    } 
    catch (Exception e) { 
     Toast.makeText(getApplicationContext(), "Please scan on the product's QR Code.", Toast.LENGTH_LONG).show(); 
    } 
    } else if (resultCode == RESULT_CANCELED) { 
    // Handle cancel 
    } 

    } 
    } 

    private OnClickListener scanListener = new OnClickListener() { 
    public void onClick(View v) { 
    try{ 
    Intent intent = new Intent("com.google.zxing.client.android.SCAN"); 
    intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); 
    startActivityForResult(intent, 0); 
    } 
    catch (Exception e){ 
    Toast.makeText(getApplicationContext(), "error opening scanner.", Toast.LENGTH_SHORT).show(); 
    } 
    } 
    }; 
} 

package com.posQR.ip; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import android.content.Context; 
import android.net.ParseException; 
import android.util.Log; 
import android.widget.Toast; 

public class MySQL{ 
double Price; 
private Context localContext; 

public void getData(String productID) { 
InputStream is = null; 
String result = ""; 

Toast.makeText(localContext.getApplicationContext(), productID, Toast.LENGTH_LONG).show(); 

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
nameValuePairs.add(new BasicNameValuePair("id",productID)); 
//http post 
try{ 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://dl.dropbox.com/u/11233767/mysqlRequest.php"); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
HttpResponse response = httpclient.execute(httppost); 
HttpEntity entity = response.getEntity(); 
is = entity.getContent(); 
}catch(Exception e){ 
    Log.e("log_tag", "Error in http connection"+e.toString()); 
} 
//convert response to string 
try { 
BufferedReader br = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
StringBuilder sb = new StringBuilder(); 
String line = null; 
while ((line = br.readLine()) != null) { 
    sb.append(line + "\n"); 
} 
is.close(); 
result = sb.toString(); 
} catch (Exception e) { 
// TODO: handle exception 
Toast.makeText(localContext.getApplicationContext(), "Error converting result.", Toast.LENGTH_LONG).show(); 
} 

//paring data 

try{ 
    JSONArray jArray = new JSONArray(result); 
    JSONObject json_data=null; 
     json_data = jArray.getJSONObject(0); 
     Price=json_data.getDouble("price"); 

    } 
    catch(JSONException e1){ 
     Toast.makeText(localContext.getApplicationContext(), "No City Found" ,Toast.LENGTH_LONG).show(); 
    } catch (ParseException e1) { 
     e1.printStackTrace(); 
} 
} 

public double getPrice(){ 
return Price; 
} 

} 
+0

任何東西在logcat?你有沒有嘗試過使用調試器? – 2012-03-26 17:36:04

+0

看起來你正在使用Android。如果是這樣,請相應標記。 – Attila 2012-03-26 17:36:52

+3

我錯過了什麼,或者你沒有初始化你的'mysql'字段?由於該方法在類中不是靜態的,因此您需要該類的一個對象。 – Pit 2012-03-26 17:40:21

回答

1

這裏有很多問題,比如:

  1. 當你onCreate使用它mysql變量爲空,則必須申報。

  2. MySql中的localContext對象從未初始化,當您撥打getData()時將導致另一個NullPointerException。您應該創建一個接受MySql類的Context對象的構造函數,並在解決第一個問題時使用該構造函數。或者,您可以將Context對象傳遞給getData()

  3. getData()方法訪問主/ UI線程中的網絡,這將導致另一個異常。在另一個線程中調用此方法或在方法本身內部創建一個新線程。你可能想使用一個AsyncTask

+0

嗨,我瞭解前2,但不完全得到它與數字3.這是什麼問題確切地從另一個類調用方法? – 2012-03-26 18:03:20

+0

您正在調用來自同一線程的getData方法,該線程是負責處理UI的主線程。這不僅是不好的做法,但Android實際上不會讓你這樣做。閱讀這篇關於Android中的線程的文章:http://developer.android.com/resources/articles/painless-threading.html – 2012-03-26 20:34:52