2012-08-28 102 views
0

這裏我從URL獲取已解析的數據,現在我試圖只在文本視圖中將解析數據的值更改爲僅在用戶上動態顯示,而我的代碼爲更改textview的值,同時改變其他textview的乘積

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) 
+0

發佈logcat輸出的其餘部分。它會告訴你零點出現在哪裏。 –

+1

@Cdr。鮑威爾你怎麼編輯它我試了很多,但無法修復它.thanx – surhidamatya

+0

@ sur007問題在這裏「resultTxt =(EditText)findViewById(R.id.editText1);」你的EditText resultTxt爲null,請檢查一次 –

回答

0

以下錯誤請確保您的佈局文件包含ID =列表一個ListView。這是ListActivity的必備條件。

<ListView android:id="@android:id/list" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#00FF00" 
      android:layout_weight="1" 
      android:drawSelectorOnTop="false"/> 

點擊here作爲參考。

在onCreateView中設置setListAdapter之前,檢查getListView()是否返回null。

+1

不,它沒有工作 – surhidamatya

+0

意味着它不爲空? –

+1

然後wat是解決方案嗎?ny idea? – surhidamatya