在这里,我从 URL 获取解析数据,现在我试图仅在文本视图上动态地将解析数据的值更改为用户,我的代码是
public class Main extends ListActivity {
EditText resultTxt;
public double C_webuserDouble;
public double C_cashDouble;
public double C_transferDouble;
public double S_webuserDouble;
public double S_cashDouble;
public double S_transferDouble;
TextView cashTxtView;
TextView webuserTxtView;
TextView transferTxtView;
TextView S_cashTxtView;
TextView S_webuserTxtView;
TextView S_transferTxtView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
cashTxtView = (TextView) findViewById(R.id.cashTxtView);
webuserTxtView = (TextView) findViewById(R.id.webuserTxtView);
transferTxtView = (TextView) findViewById(R.id.transferTxtView);
S_cashTxtView = (TextView) findViewById(R.id.S_cashTxtView);
S_webuserTxtView = (TextView) findViewById(R.id.S_webuserTxtView);
S_transferTxtView = (TextView) findViewById(R.id.S_transferTxtView);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONfunctions
.getJSONfromURL("http://ldsclient.com/ftp/strtojson.php");
try {
JSONArray netfoxlimited = json.getJSONArray("netfoxlimited");
for (inti = 0; i < netfoxlimited.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = netfoxlimited.getJSONObject(i);
map.put("date", e.getString("date"));
map.put("c_web", e.getString("c_web"));
map.put("c_bank", e.getString("c_bank"));
map.put("c_cash", e.getString("c_cash"));
map.put("s_web", e.getString("s_web"));
map.put("s_bank", e.getString("s_bank"));
map.put("s_cash", e.getString("s_cash"));
mylist.add(map);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.main,
new String[] { "date", "c_web", "c_bank", "c_cash", "s_web",
"s_bank", "s_cash", }, new int[] { R.id.item_title,
R.id.webuserTxtView, R.id.transferTxtView,
R.id.cashTxtView, R.id.S_webuserTxtView,
R.id.S_transferTxtView, R.id.S_cashTxtView });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv
.getItemAtPosition(position);
Toast.makeText(Main.this,
"ID '" + o.get("id") + "' was clicked.",
Toast.LENGTH_SHORT).show();
}
});
resultTxt = (EditText) findViewById(R.id.editText1);
resultTxt.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
resultTxt.setText("");
}
});
resultTxt.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
String text;
text = resultTxt.getText().toString();
if (resultTxt.getText().length() > 5) {
calculateSum(C_webuserDouble, C_cashDouble,
C_transferDouble);
calculateSunrise(S_webuserDouble, S_cashDouble,
S_transferDouble);
} else {
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
});
}
private void calculateSum(Double webuserDouble, Double cashDouble,
Double transferDouble) {
String Qty;
Qty = resultTxt.getText().toString();
if (Qty.length() > 0) {
double QtyValue = Double.parseDouble(Qty);
double cashResult;
double webuserResult;
double transferResult;
cashResult = cashDouble * QtyValue;
webuserResult = webuserDouble * QtyValue;
transferResult = transferDouble * QtyValue;
DecimalFormat df = new DecimalFormat("#.##");
String cashResultStr = df.format(cashResult);
String webuserResultStr = df.format(webuserResult);
String transferResultStr = df.format(transferResult);
cashTxtView.setText(String.valueOf(cashResultStr));
webuserTxtView.setText(String.valueOf(webuserResultStr));
transferTxtView.setText(String.valueOf(transferResultStr));
// cashTxtView.setFilters(new InputFilter[] {new
// DecimalDigitsInputFilter(2)});
}
if (Qty.length() == 0) {
cashTxtView.setText(String.valueOf(cashDouble));
webuserTxtView.setText(String.valueOf(webuserDouble));
transferTxtView.setText(String.valueOf(transferDouble));
}
}
private void calculateSunrise(Double webuserDouble, Double cashDouble,
Double transferDouble) {
String Qty;
Qty = resultTxt.getText().toString();
if (Qty.length() > 0) {
double QtyValue = Double.parseDouble(Qty);
double cashResult;
double webuserResult;
double transferResult;
cashResult = cashDouble * QtyValue;
webuserResult = webuserDouble * QtyValue;
transferResult = transferDouble * QtyValue;
DecimalFormat df = new DecimalFormat("#.##");
String cashResultStr = df.format(cashResult);
String webuserResultStr = df.format(webuserResult);
String transferResultStr = df.format(transferResult);
S_cashTxtView.setText(String.valueOf(cashResultStr));
S_webuserTxtView.setText(String.valueOf(webuserResultStr));
S_transferTxtView.setText(String.valueOf(transferResultStr));
}
if (Qty.length() == 0) {
S_cashTxtView.setText(String.valueOf(cashDouble));
S_webuserTxtView.setText(String.valueOf(webuserDouble));
S_transferTxtView.setText(String.valueOf(transferDouble));
}
}
}
我在 logcat 上收到以下错误
08-28 15:04:12.839: E/AndroidRuntime(584): Uncaught handler: thread main exiting due to uncaught exception
08-28 15:04:12.848: E/AndroidRuntime(584): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mokshya.jsontutorialhos.xmltest/com.mokshya.jsontutorial.Main}: java.lang.NullPointerException
08-28 15:04:12.848: E/AndroidRuntime(584): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
08-28 15:04:12.848: E/AndroidRuntime(584): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
08-28 15:04:12.848: E/AndroidRuntime(584): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
08-28 15:04:12.848: E/AndroidRuntime(584): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
08-28 15:04:12.848: E/AndroidRuntime(584): at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 15:04:12.848: E/AndroidRuntime(584): at android.os.Looper.loop(Looper.java:123)
08-28 15:04:12.848: E/AndroidRuntime(584): at android.app.ActivityThread.main(ActivityThread.java:4203)
08-28 15:04:12.848: E/AndroidRuntime(584): at java.lang.reflect.Method.invokeNative(Native Method)
08-28 15:04:12.848: E/AndroidRuntime(584): at java.lang.reflect.Method.invoke(Method.java:521)
08-28 15:04:12.848: E/AndroidRuntime(584): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
08-28 15:04:12.848: E/AndroidRuntime(584): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
08-28 15:04:12.848: E/AndroidRuntime(584): at dalvik.system.NativeStart.main(Native Method)
08-28 15:04:12.848: E/AndroidRuntime(584): Caused by: java.lang.NullPointerException
08-28 15:04:12.848: E/AndroidRuntime(584): at com.mokshya.jsontutorial.Main.onCreate(Main.java:111)
08-28 15:04:12.848: E/AndroidRuntime(584): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
08-28 15:04:12.848: E/AndroidRuntime(584): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)