1

我正在使用片段创建一个应用程序。我有在活动中工作的代码,但相同的代码在片段中不起作用。这是我的代码:

package com.example.currencyconverter;


import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.DecimalFormat;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Fragment;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;


public class MainActivity extends Fragment implements OnClickListener {

    ProgressDialog progressBar;
    EditText edittextdollars;
    EditText edittexteuros;
    TextView dollars;
    TextView euros;
    RadioButton dtoe;
    RadioButton etod;
    RadioGroup radiogroup;
    Spinner spinner1;
    Spinner spinner2;
    Button calculate;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_main, container, false);

    }

    private void addListenerOnButton() {
        spinner1 = (Spinner) getView().findViewById(R.id.spinner1);
        spinner2 = (Spinner) getView().findViewById(R.id.spinner2);
        calculate = (Button) getView().findViewById(R.id.calculate);
        edittextdollars = (EditText) getView().findViewById(R.id.edittextdollars);
        edittexteuros = (EditText) getView().findViewById(R.id.edittexteuros);
    }

    public interface CurrencyConverter {
        public double convert(String currencyFrom, String currencyTo) throws Exception;
    }

    public class YahooCurrencyConverter implements CurrencyConverter {
        public double convert(String currencyFrom, String currencyTo) throws IOException {
            HttpClient httpclient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet("http://quote.yahoo.com/d/quotes.csv?s=" + currencyFrom + currencyTo + "=X&f=l1&e=.csv");
            ResponseHandler < String > responseHandler = new BasicResponseHandler();
            String responseBody = httpclient.execute(httpGet, responseHandler);
            httpclient.getConnectionManager().shutdown();
            return Double.parseDouble(responseBody);
        }


    }

    @Override
    public void onClick(View v) {
        String text1 = spinner1.getSelectedItem().toString().trim();
        String text2 = spinner2.getSelectedItem().toString().trim();


        if (text1.equals("US Dollar - USD") && text2.equals("Euro - EUR") && edittextdollars.length() > 0 && edittexteuros.length() == 0) {
            convertvalues("USD", "EUR");
        }
        if (text1.equals("US Dollar - USD") && text2.equals("Euro - EUR") && edittexteuros.length() > 0 && edittextdollars.length() == 0) {
            convertvalues2("EUR", "USD");
        }
        if (text1.equals("Euro - EUR") && text2.equals("US Dollar - USD") && edittextdollars.length() > 0 && edittexteuros.length() == 0) {
            convertvalues("EUR", "USD");
        }
        if (text1.equals("Euro - EUR") && text2.equals("US Dollar - USD") && edittexteuros.length() > 0 && edittextdollars.length() == 0) {
            convertvalues2("USD", "EUR");
        }
        if (text1.equals("Euro - EUR") && text2.equals("Euro - EUR") && edittextdollars.length() > 0 && edittexteuros.length() == 0) {
            convertEurostoEuros();
        }
        if (text1.equals("Euro - EUR") && text2.equals("Euro - EUR") && edittexteuros.length() > 0 && edittextdollars.length() == 0) {
            OppositeOfConvertEurostoEuros();
        }
        if (text1.equals("US Dollar - USD") && text2.equals("US Dollar - USD") && edittextdollars.length() > 0 && edittexteuros.length() == 0) {
            convertDollarstoDollars();
        }
        if (text1.equals("US Dollar - USD") && text2.equals("US Dollar - USD") && edittexteuros.length() > 0 && edittextdollars.length() == 0) {
            OppositeOfConvertDollarstoDollars();
        }
        if (text1.equals("Indian Rupee - INR") && text2.equals("Indian Rupee - INR") && edittextdollars.length() > 0 && edittexteuros.length() == 0) {
            convertEurostoEuros();
        }
        if (text1.equals("Indian Rupee - INR") && text2.equals("Indian Rupee - INR") && edittexteuros.length() > 0 && edittextdollars.length() == 0) {
            OppositeOfConvertDollarstoDollars();
        }
        if (text1.equals("Indian Rupee - INR") && text2.equals("US Dollar - USD") && edittextdollars.length() > 0 && edittexteuros.length() == 0) {
            convertvalues("INR", "USD");
        }
        if (text1.equals("Indian Rupee - INR") && text2.equals("US Dollar - USD") && edittexteuros.length() > 0 && edittextdollars.length() == 0) {
            convertvalues2("USD", "INR");
        }
        if (text1.equals("Indian Rupee - INR") && text2.equals("Euro - EUR") && edittextdollars.length() > 0 && edittexteuros.length() == 0) {
            convertvalues("INR", "EUR");
        }
        if (text1.equals("Indian Rupee - INR") && text2.equals("Euro - EUR") && edittexteuros.length() > 0 && edittextdollars.length() == 0) {
            convertvalues2("EUR", "INR");
        }
        if (text1.equals("US Dollar - USD") && text2.equals("Indian Rupee - INR") && edittextdollars.length() > 0 && edittexteuros.length() == 0) {
            convertvalues("USD", "INR");
        }
        if (text1.equals("US Dollar - USD") && text2.equals("Indian Rupee - INR") && edittexteuros.length() > 0 && edittextdollars.length() == 0) {
            convertvalues2("INR", "USD");
        }
        if (text1.equals("Euro - EUR") && text2.equals("Indian Rupee - INR") && edittexteuros.length() > 0 && edittextdollars.length() == 0) {
            convertvalues2("INR", "EUR");
        }
        if (text1.equals("Euro - EUR") && text2.equals("Indian Rupee - INR") && edittextdollars.length() > 0 && edittexteuros.length() == 0) {
            convertvalues("EUR", "INR");
        }



        if (text1.equals("Japanese Yen - JPY") && text2.equals("Japanese Yen - JPY") && edittextdollars.length() > 0 && edittexteuros.length() == 0) {
            convertvalues("JPY", "JPY");
        }
        if (text1.equals("Japanese Yen - JPY") && text2.equals("Japanese Yen - JPY") && edittexteuros.length() > 0 && edittextdollars.length() == 0) {
            convertvalues2("JPY", "JPY");
        }
        if (text1.equals("Japanese Yen - JPY") && text2.equals("US Dollar - USD") && edittextdollars.length() > 0 && edittexteuros.length() == 0) {
            convertvalues("JPY", "USD");
        }
        if (text1.equals("Japanese Yen - JPY") && text2.equals("US Dollar - USD") && edittexteuros.length() > 0 && edittextdollars.length() == 0) {
            convertvalues2("USD", "JPY");
        }
        if (text1.equals("Japanese Yen - JPY") && text2.equals("Indian Rupee - INR") && edittextdollars.length() > 0 && edittexteuros.length() == 0) {
            convertvalues("JPY", "INR");
        }
        if (text1.equals("Japanese Yen - JPY") && text2.equals("Indian Rupee - INR") && edittexteuros.length() > 0 && edittextdollars.length() == 0) {
            convertvalues2("INR", "JPY");
        }
        if (text1.equals("Japanese Yen - JPY") && text2.equals("Euro - EUR") && edittextdollars.length() > 0 && edittexteuros.length() == 0) {
            convertvalues("JPY", "EUR");
        }
        if (text1.equals("Japanese Yen - JPY") && text2.equals("Euro - EUR") && edittexteuros.length() > 0 && edittextdollars.length() == 0) {
            convertvalues2("EUR", "JPY");
        }



        if (text1.equals("US Dollar - USD") && text2.equals("Japanese Yen - JPY") && edittextdollars.length() > 0 && edittexteuros.length() == 0) {
            convertvalues("USD", "JPY");
        }
        if (text1.equals("US Dollar - USD") && text2.equals("Japanese Yen - JPY") && edittexteuros.length() > 0 && edittextdollars.length() == 0) {
            convertvalues2("JPY", "USD");
        }
        if (text1.equals("Euro - EUR") && text2.equals("Japanese Yen - JPY") && edittextdollars.length() > 0 && edittexteuros.length() == 0) {
            convertvalues("EUR", "JPY");
        }
        if (text1.equals("Euro - EUR") && text2.equals("Japanese Yen - JPY") && edittexteuros.length() > 0 && edittextdollars.length() == 0) {
            convertvalues2("JPY", "EUR");
        }
        if (text1.equals("Indian Rupee - INR") && text2.equals("Japanese Yen - JPY") && edittextdollars.length() > 0 && edittexteuros.length() == 0) {
            convertvalues("INR", "JPY");
        }
        if (text1.equals("Indian Rupee - INR") && text2.equals("Japanese Yen - JPY") && edittexteuros.length() > 0 && edittextdollars.length() == 0) {
            convertvalues2("JPY", "INR");
        } else {
            System.out.println("");
        }
    }

    public String convertvalues(String convertfrom, String convertto) {
        double current;
        double val = Double.parseDouble(edittextdollars.getText().toString());
        DecimalFormat df = new DecimalFormat(".##");
        YahooCurrencyConverter ycc = new YahooCurrencyConverter();
        try {
            current = ycc.convert(convertfrom, convertto);
            edittexteuros.setText(df.format(val * current));
            return "passed";
        } catch (Exception e) {
            StringWriter errors = new StringWriter();
            e.printStackTrace(new PrintWriter(errors));
            return errors.toString();
        }
    }

    public String convertvalues2(String convertfrom2, String convertto2) {
        double current;
        double val = Double.parseDouble(edittexteuros.getText().toString());
        DecimalFormat df = new DecimalFormat(".##");
        YahooCurrencyConverter ycc = new YahooCurrencyConverter();
        try {
            current = ycc.convert(convertfrom2, convertto2);
            edittextdollars.setText(df.format(val * current));
            return "passed";
        } catch (Exception e) {
            StringWriter errors = new StringWriter();
            e.printStackTrace(new PrintWriter(errors));
            return errors.toString();
        }

    }

    protected void convertEurostoDollars() {
        double val = Double.parseDouble(edittexteuros.getText().toString());
        DecimalFormat df = new DecimalFormat(".##");
        edittextdollars.setText(df.format(val * 1.33));

    }

    protected void convertEurostoEuros() {
        double val = Double.parseDouble(edittextdollars.getText().toString());
        DecimalFormat df = new DecimalFormat(".##");
        edittexteuros.setText(df.format(val * 1));
    }

    protected void convertDollarstoDollars() {
        double val = Double.parseDouble(edittextdollars.getText().toString());
        DecimalFormat df = new DecimalFormat(".##");
        edittexteuros.setText(df.format(val * 1));
    }

    protected void OppositeOfConvertEurostoDollars() {
        double val = Double.parseDouble(edittextdollars.getText().toString());
        DecimalFormat df = new DecimalFormat(".##");
        edittexteuros.setText(df.format(val * 1.33));
    }

    protected void OppositeOfConvertDollarstoEuros() {
        double val = Double.parseDouble(edittexteuros.getText().toString());
        DecimalFormat df = new DecimalFormat(".##");
        edittextdollars.setText(df.format(val * 0.75));
    }

    protected void OppositeOfConvertEurostoEuros() {
        double val = Double.parseDouble(edittexteuros.getText().toString());
        DecimalFormat df = new DecimalFormat(".##");
        edittextdollars.setText(df.format(val * 1));
    }

    protected void OppositeOfConvertDollarstoDollars() {
        double val = Double.parseDouble(edittexteuros.getText().toString());
        DecimalFormat df = new DecimalFormat(".##");
        edittextdollars.setText(df.format(val * 1));
    }
}

这是我的 xml 按钮:

<Button
android:id="@+id/calculate"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/edittexteuros"
android:text="@string/calculate" 
android:onClick="onClick"/>

作为额外参考,这里是我的应用程序崩溃的 LogCat:

06-30 01:45:56.807: E/AndroidRuntime(1752): FATAL EXCEPTION: main
06-30 01:45:56.807: E/AndroidRuntime(1752): java.lang.IllegalStateException: Could not find a method onClick(View) in the activity class com.example.currencyconverter.MainActivity2 for onClick handler on view class android.widget.Button with id 'calculate'
06-30 01:45:56.807: E/AndroidRuntime(1752):     at android.view.View$1.onClick(View.java:3586)
06-30 01:45:56.807: E/AndroidRuntime(1752):     at android.view.View.performClick(View.java:4204)
06-30 01:45:56.807: E/AndroidRuntime(1752):     at android.view.View$PerformClick.run(View.java:17355)
06-30 01:45:56.807: E/AndroidRuntime(1752):     at android.os.Handler.handleCallback(Handler.java:725)
06-30 01:45:56.807: E/AndroidRuntime(1752):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-30 01:45:56.807: E/AndroidRuntime(1752):     at android.os.Looper.loop(Looper.java:137)
06-30 01:45:56.807: E/AndroidRuntime(1752):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-30 01:45:56.807: E/AndroidRuntime(1752):     at java.lang.reflect.Method.invokeNative(Native Method)
06-30 01:45:56.807: E/AndroidRuntime(1752):     at java.lang.reflect.Method.invoke(Method.java:511)
06-30 01:45:56.807: E/AndroidRuntime(1752):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-30 01:45:56.807: E/AndroidRuntime(1752):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-30 01:45:56.807: E/AndroidRuntime(1752):     at dalvik.system.NativeStart.main(Native Method)
06-30 01:45:56.807: E/AndroidRuntime(1752): Caused by: java.lang.NoSuchMethodException: onClick [class android.view.View]
06-30 01:45:56.807: E/AndroidRuntime(1752):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
06-30 01:45:56.807: E/AndroidRuntime(1752):     at java.lang.Class.getMethod(Class.java:915)
06-30 01:45:56.807: E/AndroidRuntime(1752):     at android.view.View$1.onClick(View.java:3579)
06-30 01:45:56.807: E/AndroidRuntime(1752):     ... 11 more

我究竟做错了什么?这里发生了什么?任何帮助是极大的赞赏。

4

1 回答 1

0

我不确定您是否缺少某些代码或什么,但是 addListenerOnButton() 方法永远不会在任何地方调用。我将假设您可能在活动的 onCreate() 中调用它。您可能还注意到,您不能在片段的 oncCreate() 中使用 getView(),因为在 onCreateView() 内部调用膨胀之前无法访问根视图。

我建议执行以下操作来解决您的问题。

首先从您的 xml 中删除 onClick 行,因为您将不再需要它,因为我们将以编程方式注册按钮。

您还可以将您在 addListernOnButton() 中所做的所有工作移到 onCreateView() 中。

View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.activity_main, container, false);
    spinner1 = (Spinner) rootView.findViewById(R.id.spinner1);
    spinner2 = (Spinner) rootView.findViewById(R.id.spinner2);
    calculate = (Button) rootView.findViewById(R.id.calculate);
    calculate.setOnClickListener(this);
    edittextdollars = (EditText) rootView.findViewById(R.id.edittextdollars);
    edittexteuros = (EditText) rootView.findViewById(R.id.edittexteuros);
    return rootView;
}
于 2013-06-30T04:04:03.187 回答