gpt4 book ai didi

java - 无法调用方法

转载 作者:行者123 更新时间:2023-12-01 15:28:31 26 4
gpt4 key购买 nike

我正在尝试将 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. mysql变量在onCreate中使用时为空,必须声明它。

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

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

关于java - 无法调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9876867/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com