0

私には4つの活動があります。

  • Demo_tabActivity.java [主なアクティビティ]

  • タブホスト.java

以下の 2 つのアクティビティは、上記の tabhost.java のタブです。

  • Tab_1.java

  • Tab_2.java

最初のアクティビティ ( Demo_tabActivity.java) には edittext とボタンが含まれます。2 番目の ( Tabhost.java) アクティビティにはタブホスト ウィジェットが含まれます。3 番目と 4 番目のアクティビティにはそれぞれテキストビューが含まれます。

最初のアクティビティは、ユーザーからの入力を取得して Web サービスを消費し、タブホスト (2 番目のアクティビティ) の最初のタブ (3 番目のアクティビティ) にデータを返します。

Webサービスの消費はうまく機能しており、値を完全に返します。

しかし、問題は、タブホストに表示するのではなく、別のページに結果を表示することです。

Demo_tabActivity.java

 public class Demo_tabActivity extends Activity 
 {

private static String NAMESPACE = "http://tempuri.org/";
   private static String METHOD_NAME = "FahrenheitToCelsius";
   private static String SOAP_ACTION = "http://tempuri.org/FahrenheitToCelsius";
   private static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";

   Button btnFar;
   EditText txtFar;

   @Override
   public void onCreate(Bundle savedInstanceState)
   {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       btnFar = (Button)findViewById(R.id.button1);

       txtFar = (EditText)findViewById(R.id.editText_in);

       btnFar.setOnClickListener(new View.OnClickListener()
       {
       public void onClick(View v)
       {
           String b;

         //Initialize soap request + add parameters
         SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);       

         //Use this to add parameters
         request.addProperty("Fahrenheit",txtFar.getText().toString());

         //Declare the version of the SOAP request
         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

         envelope.setOutputSoapObject(request);
         envelope.dotNet = true;

         try 
         {
             HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

             //this is the actual part that will call the webservice
             androidHttpTransport.call(SOAP_ACTION, envelope);

             // Get the SoapResult from the envelope body.

             SoapPrimitive result = (SoapPrimitive)envelope.getResponse();

             if(result != null)
             {
              //Get the first property and change the label text

                b = result.toString();
                Intent itnt = new Intent(v.getContext(), Tab_1.class);
                itnt.putExtra("gotonextpage", b.toString());
                startActivity(itnt);
             }
             else
             {
           Toast.makeText(getApplicationContext(), "NoResponse",Toast.LENGTH_SHORT).show();
             }
         }
        catch (Exception e)
        {
           e.printStackTrace();
           }
         }
       });
       }

注:上記のコードのif条件についてのみ疑問があります


Tab_1.java

public class Tab_1 extends Activity 
{
TextView tv;
String result;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);

Bundle extras = getIntent().getExtras();
if(extras != null)
{   result = extras.getString("gotonextpage");  }
tv = (TextView)findViewById(R.id.textView_main2);
tv.setText(result);
}}

タブホスト.java

 public class Tabhost extends TabActivity {

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main1);


    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec;
    Intent intent;

    intent = new Intent().setClass(this, Tabhost.class);
    spec = tabHost.newTabSpec("first").setIndicator("First").setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, Tab_2.class);
    spec = tabHost.newTabSpec("second").setIndicator("Second").setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
    }
}
4

1 に答える 1

0

あなたの問題は、タブホストに Tab_1 を含めていないためです (そして、TabHost を自己参照しています...それがクラッシュを引き起こしていないかどうかはわかりません...)。

私はこの行を信じています:

intent = new Intent().setClass(this, Tabhost.class); 

これに変更する必要があります (またはそれに別の TabSpec を追加します):

intent = new Intent().setClass(this, Tab_1.class); 
于 2012-06-22T13:23:31.397 に答える