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
什麼我做錯了嗎?這裏發生了什麼?任何幫助是極大的讚賞。
您是否嘗試以編程方式爲該按鈕創建'onClick'偵聽器? –
是的,我有,但它仍然不起作用 – user2507301
當你做了嗎? –