2012-06-22 82 views
0

在tabhost的標籤數據我有即四項活動,無法顯示在Android

  • Demo_tabActivity.java [主要活動]

  • Tabhost.java

的下面兩個活動是上述tabhost的選項卡.java

  • Tab_1.java

  • Tab_2.java

第一個活動(Demo_tabActivity.java)包含一個EditText & button.The第二(Tabhost.java)活性包含Tabhost widget.The第三&第四活動包含textviews分別。

第一個活動將通過從用戶獲取輸入並返回tabhost(第二個活動)的第一個選項卡(第三個活動)上的某些數據來使用Web服務。

web服務的好消費工作狀態良好,完全返回值,

,但問題是,它顯示了單獨的頁面,而不是顯示在tabhost的結果。

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

注:我唯一的疑問。如果條件在上面的代碼


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

Tabhost.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); 
    } 
} 
+0

您shuld發表您的tabhost代碼了。 – Barak

+0

@Barak請再次訪問我的問題,我已經包括tabhost.java –

回答

0

你的問題,是因爲你不包括tab_1中你tabhost(你是自參考TabHost ...不知道怎麼說不是導致飛機墜毀... )。

我相信這行:需要

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

改變這個(或添加另一個則tabspec吧):

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