无法调用方法

时间:2012-03-26 17:33:30

标签: java android eclipse

我正在尝试将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;
}

}

1 个答案:

答案 0 :(得分:1)

这里有很多问题,例如:

  1. mysql变量在onCreate中使用时为空,您必须声明它。

  2. MySql中的localContext对象永远不会被初始化,当您调用NullPointerException时,这将导致另一个getData()。您应该创建一个构造函数,该构造函数接受MySql类的Context对象,并在修复第一个问题时使用该构造函数。或者,您可以将Context对象传递给getData()

  3. getData()方法访问主/ UI线程中的网络,这将导致另一个异常。在另一个线程中调用此方法或在方法本身中分离新线程。您可能想要使用AsyncTask