0

我有四项活动,即

  • Demo_tabActivity.java [主要活动]

  • Tabhost.java

下面两个activity是上面tabhost.java的标签

  • Tab_1.java

  • Tab_2.java

第一个活动(Demo_tabActivity.java)包含一个编辑文本和按钮。第二个(Tabhost.java)活动包含一个 Tabhost 小部件。第三个和第四个活动分别包含文本视图。

第一个活动将通过获取用户的输入来使用 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();
           }
         }
       });
       }

注意:我只怀疑上面代码中的 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);
}}

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

1 回答 1

0

您的问题是因为您没有在 tabhost 中包含 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 回答