0

私は Android アプリケーションを書いています。私のコードは通貨コンバーターです。変換したい金額を入力しても何も変わりません。変換された金額はゼロです。

コードの何が問題なのかわかりません:

ここに私が書いているコードがあります:

public class Currency_convert extends Activity
{
    /** Called when the activity is first created. */

    float inputValue;
    Handler h;
    static int decimalsize = 2;
    public DecimalFormat df = new DecimalFormat();

    String US = "USD";
    String EURO = "EUR";
    String INR = "INR";
    String AUD = "AUD";
    String GBP = "GBP";

    public double dUS;// = 0.0;
    public double dEURO = 1.0;
    public double dINR;// = 0.0;
    public double dAUD;// = 0.0;
    public double dGBP;// = 0.0;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.currency);
        boolean rateupdate = false;

        AbsoluteLayout al = (AbsoluteLayout) findViewById(R.id.AbsoluteLayout01);
        al.setBackgroundColor(Color.LTGRAY);

        final EditText amount = (EditText) findViewById(R.id.txt1);
        final EditText etResult = (EditText) findViewById(R.id.txt2);
        final Spinner sp1 = (Spinner) findViewById(R.id.spinerr1);
        final Spinner sp2 = (Spinner) findViewById(R.id.spinerr2);

        final InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.currency, android.R.layout.simple_spinner_item);
        final Button btn = (Button) findViewById(R.id.bb1);

        amount.setText("0.0");
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
     //   adapter.setDropDownViewResource(android.R.layout.)
        sp1.setAdapter(adapter);
        sp2.setAdapter(adapter);

        rateupdate = getRate();
        if(rateupdate)
            btn.setText("Currency Rate updated");
        else
            btn.setText("Updated Currency Rate ");

        //btn.setText(Boolean.toString(rateupdate));

        OnClickListener ltouch = new OnClickListener() 
        {
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub
                //getRate();
            }
        };;;
        btn.setOnClickListener(ltouch );

        OnItemClickListener itemClick = new OnItemClickListener() 
        {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,long id) 
            {

                int curr1 = (int) sp1.getSelectedItemId();
                int curr2 = (int) sp2.getSelectedItemId();
                double result = Convert(curr1, curr2, amount.getText().toString());

                etResult.setText(Double.toString(result));

            }
        };

        OnItemSelectedListener ItemSelectListener = new OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,int position, long id) 
            {
                try
                {

                    int curr1 = (int) sp1.getSelectedItemId();
                    int curr2 = (int) sp2.getSelectedItemId();
                    double result = Convert(curr1, curr2, amount.getText().toString());
                    etResult.setText(Double.toString(result));

                }
                catch(Exception e)
                {

                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) 
            {
                // TODO Auto-generated method stub

            }
        };
        sp1.setOnItemSelectedListener(ItemSelectListener);
        sp2.setOnItemSelectedListener(ItemSelectListener);

        amount.addTextChangedListener(new TextWatcher() 
        {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) 
            {
                // TODO Auto-generated method stub
                int length = s.length();
                if(length == 0)
                    //amount.setText("0.0");
                    s = "0.0";
                int curr1 = (int) sp1.getSelectedItemId();
                int curr2 = (int) sp2.getSelectedItemId();
                //double result = Convert(curr1, curr2, amount.getText().toString());
                double result = Convert(curr1, curr2, s.toString());

                etResult.setText(Double.toString(result));      
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,int after) 
            {
                // TODO Auto-generated method stub
            }
            @Override
            public void afterTextChanged(Editable s) 
            {
                // TODO Auto-generated method stub
            }
        });
        OnTouchListener l = new OnTouchListener() 
        {
            @Override
            public boolean onTouch(View v, MotionEvent event) 
            {
                // TODO Auto-generated method stub
                imm.hideSoftInputFromWindow(amount.getWindowToken(),0);//
                return true;
            }
        }; 
        al.setOnTouchListener(l);
        sp1.setSelection(4);
        sp2.setSelection(0);
    }

    protected Double Convert(int currency_from_index, int currency_to_index, String s_amount) 
    {
        // TODO Auto-generated method stub
        Double result = 0.0;
        Double amount = Double.parseDouble(s_amount);
        Double source = 0.0;
        Double destination = 0.0;

        if((dUS * dAUD * dINR * dGBP * dEURO) == 0)
        {
            result = 0.0;
            return result;
        }
        else
        {

            switch (currency_from_index) 
            {
            case 0:
                source = dUS;
                break;
            case 1:
                source = dAUD;
                break;
            case 2:
                source = dINR;
                break;
            case 3:
                source = dGBP;
                break;
            case 4:
                source = dEURO;
                break;
            default:
                break;
            }

            switch (currency_to_index)
            {
            case 0:
                destination = dUS;
                break;
            case 1:
                destination = dAUD;
                break;
            case 2:
                destination = dINR;
                break;
            case 3:
                destination = dGBP;
                break;
            case 4:
                destination = dEURO;
                break;
            default:
                break;
            }

            result = amount*destination/source;
            Math.round(result);
            try
            {
            df.setMaximumFractionDigits(decimalsize);   
            df.setMinimumFractionDigits(decimalsize);
            String str_result = df.format(result);
            result = Double.parseDouble(str_result);
            }
            catch(Exception e)          
            {

            }

            return result;
        }

    }


    public boolean getRate()
    {
        //ConversionRate cr = new ConversionRate();
        try
        {

         //URL url = new URL(eText.getText().toString());
            String path = "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml";
        URL url = new URL(path);
        URLConnection conn = url.openConnection();
        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();

        //xpp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) );
        xpp.setInput(rd);
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) 
        {
         if(eventType == XmlPullParser.START_DOCUMENT) 
         {
             //System.out.println("Start document");
             //tView.append("Start document");
         } 
         else if(eventType == XmlPullParser.END_DOCUMENT) 
         {
             //System.out.println("End document");
             //tView.append("End document");
         } 
         else if(eventType == XmlPullParser.START_TAG) 
         {
             //System.out.println("Start tag "+xpp.getName());
             //tView.append("Start tag "+xpp.getName());
             //tView.append("Attribute Count = " + xpp.getAttributeCount());
             if(xpp.getAttributeCount() > 1)
             {
                 /*tView.append("Currency = " + xpp.getAttributeValue(0) + "\t");
                 tView.append("Rate = " + xpp.getAttributeValue(1));
                 tView.append("\n");*/
                 //cr.setName(xpp.getAttributeValue(0));
                 //cr.setRate(Double.parseDouble(xpp.getAttributeValue(1)));
                 if(xpp.getAttributeValue(0).equals(US))// toString()== US)
                     Currency_convert.this.dUS = Double.parseDouble(xpp.getAttributeValue(1));
                 else if(xpp.getAttributeValue(0).equals(INR))// .toString() == "INR")
                     dINR = Double.parseDouble(xpp.getAttributeValue(1));
                 else if(xpp.getAttributeValue(0).equals(GBP))// .toString() == "GBP")
                     dGBP = Double.parseDouble(xpp.getAttributeValue(1));
                 else if(xpp.getAttributeValue(0).equals(AUD))// .toString() == "AUD")
                     dAUD = Double.parseDouble(xpp.getAttributeValue(1));
             } 
         } 
         else if(eventType == XmlPullParser.END_TAG) 
         {
             //System.out.println("End tag "+xpp.getName());
             //tView.append("End tag "+xpp.getName());
         } 
         else if(eventType == XmlPullParser.TEXT) 
         {
             //System.out.println("Text "+xpp.getText());
             //tView.append("Text "+xpp.getText());
         }
         eventType = xpp.next();
        }
        return true;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return false;
        }

    }

}

ここにxmlファイルがあります:

<?xml version="1.0" encoding="utf-8"?>




<AbsoluteLayout android:id="@+id/AbsoluteLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<EditText android:id="@+id/txt1" android:text="@+id/EditText01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_y="10dip" android:numeric="decimal" android:singleLine="true" android:ems="8" android:hint="Enter amount" android:layout_x="5dip"></EditText>
<EditText android:id="@+id/txt2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@+id/EditText02" android:editable="false" android:enabled="true" android:singleLine="true" android:layout_x="5dip" android:ems="8" android:layout_y="69dip"></EditText>

<Spinner android:id="@+id/spinerr1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_y="10dip" android:layout_x="180dip"></Spinner>
<Spinner android:layout_y="69dip" android:id="@+id/spinerr2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_x="180dip"></Spinner>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/bb1" android:layout_x="50dip" android:text="Update Currency Rates" android:layout_y="120dip"></Button>
</AbsoluteLayout>

LogCat でエラーが発生しません!!!

4

1 に答える 1

0

コードには多くのエラーがあります:

1.推奨されていない絶対レイアウトを使用しないでください。

2.このコードのコメントを削除します。

OnClickListener ltouch = new OnClickListener() 
    {
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            //getRate();
        }
    };;;

3. AsncTask または Thread を使用してバックグラウンド操作を実行します。

于 2013-11-07T10:16:29.330 に答える