0

我在我的 Android 应用程序中遇到了问题。这是一个货币转换器应用程序。当我从选项卡开始新活动时,该活动为空白。这是我的 MainActivity.java 代码的一部分:

    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.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity implements OnClickListener {

ProgressDialog progressBar;
private int progressBarStatus = 0;
EditText edittextdollars;
EditText edittexteuros;
TextView dollars;
TextView euros;
RadioButton dtoe;
RadioButton etod;
RadioGroup radiogroup;
Spinner spinner1;
Spinner spinner2;
Button calculate;
String[] tabTitle = {"Converter", "Currencies", "News"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    addListenerOnButton();
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    final ActionBar actionBar = getActionBar();
    actionBar.setSubtitle("Created By Rohit Nandakumar");
    actionBar.setTitle("Currency Converter"); 
    // Specify that tabs should be displayed in the action bar.
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create a tab listener that is called when the user changes tabs.
    ActionBar.TabListener tabListener = new ActionBar.TabListener() {
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
            String n = tab.getText().toString();
            Toast.makeText(getApplicationContext(), "You have selected: " + n, Toast.LENGTH_LONG).show();
            if (n.equals("Converter")) {
                startActivity(new Intent (MainActivity.this, MainActivity.class));  
                } 
            if (n.equals("Currencies")) {
                startActivity(new Intent (MainActivity.this, FirstActivity.class)); 
                }
            if (n.equals("News")) {
                startActivity(new Intent (MainActivity.this, FirstActivity.class)); 
                }
        }

        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
            // hide the given tab
        }

        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
            // probably ignore this event
        }
    };

    // Add 3 tabs, specifying the tab's text and TabListener
    for (int i = 0; i < 3; i++) {
        actionBar.addTab(
                actionBar.newTab()
                        .setText(tabTitle[i])
                        .setTabListener(tabListener));
    }




}


private void addListenerOnButton() {
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    calculate = (Button) findViewById(R.id.calculate);
    edittextdollars = (EditText) findViewById(R.id.edittextdollars);
    edittexteuros = (EditText) 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");
            //HttpGet httpGet = new HttpGet("http://quote.yahoo.com/d/quotes.csv?s=USDEUR=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();
        progressBar = new ProgressDialog(v.getContext());
        progressBar.setCancelable(true);
        progressBar.setMessage("File downloading ...");
        progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressBar.setProgress(0);
        progressBar.setMax(100);
        progressBar.show();

        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("");
        }
        progressBar.hide();
    }



/* public void convertDollarstoEuros() {
    double current;
    double val = Double.parseDouble(edittextdollars.getText().toString());
    DecimalFormat df = new DecimalFormat(".##");
     YahooCurrencyConverter ycc = new YahooCurrencyConverter();
     //current = ycc.convert("USD", "EUR");

    //edittexteuros.setText(df.format(val*current)); 
     //edittexteuros.setText(df.format(6));

         try {
            current = ycc.convert("USD", "EUR");
            //System.out.println(current);
            edittexteuros.setText(df.format(val*current)); 
            }
        catch (Exception e) {
            StringWriter errors = new StringWriter();
            e.printStackTrace(new PrintWriter(errors));
            //return errors.toString();
        }
        //    e.printStackTrace();
            //edittexteuros.setText(df.format(6));
        } 


         public String convertDollarstoEuro1() {
            double current;
            double val = Double.parseDouble(edittextdollars.getText().toString());
            DecimalFormat df = new DecimalFormat(".##");
             YahooCurrencyConverter ycc = new YahooCurrencyConverter();
              //current = ycc.convert("USD", "EUR");

            //edittexteuros.setText(df.format(val*current)); 
             //edittexteuros.setText(df.format(6));

                 try {
                    current = ycc.convert("USD", "EUR");
                    //System.out.println(current);
                    edittexteuros.setText(df.format(val*current)); 
                    return "passed";
                    }
                catch (Exception e) {
                    StringWriter errors = new StringWriter();
                    e.printStackTrace(new PrintWriter(errors));
                    return errors.toString();
                //    e.printStackTrace();
                    //edittexteuros.setText(df.format(6));
                } 
         }
         */

                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();
                        } 

                }


}

这是我的 AndroidManifest 文件:

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

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.currencyconverter"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<supports-screens android:largeScreens="true"
                android:normalScreens="true"
                android:smallScreens="true"
                android:xlargeScreens="true" />
<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.currencyconverter.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.currencyconverter.FirstActivity"
        android:label="@string/title_activity_first" >
    </activity>
    <activity
        android:name="com.example.currencyconverter.OtherMainActivity"
        android:label="@string/title_activity_other_main" >
    </activity>
</application>

我也命名了所有活动。发生了什么?为什么我的应用程序无法运行?任何帮助将不胜感激

4

1 回答 1

1

请使用此代码或为新视图设置布局文件。

public void onTabSelected(Tab tab, FragmentTransaction ft) {

ft.replace(R.id.layoutfile,片段);}

于 2013-06-25T01:54:23.837 回答