TabChange上的调用方法

时间:2011-07-22 15:11:33

标签: android android-activity call

我试图在Tab-Change上刷新布局但是我失败了:

主要活动

package com.hello;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.*;
import android.widget.*;
import android.widget.TabHost.OnTabChangeListener;


public class HelloAndroidActivity extends TabActivity {


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

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, WatchActivity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("watch").setIndicator("Watch",
                      res.getDrawable(R.drawable.watch))
                  .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, SettingsActivity.class);
    spec = tabHost.newTabSpec("settings").setIndicator("Settings",
                      res.getDrawable(R.drawable.settings))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, ResultsActivity.class);
    spec = tabHost.newTabSpec("results").setIndicator("Results",
                      res.getDrawable(R.drawable.results))
                  .setContent(intent);
    tabHost.addTab(spec);


    tabHost.setOnTabChangedListener(new OnTabChangeListener() {

        public void onTabChanged(String tabId) {

            WatchActivity s = new WatchActivity();
            s.setBoatsinWatch();

            }


    });


    tabHost.setCurrentTab(0);
}
}

观看活动:

  public class WatchActivity extends Activity {      
    List<String> boat_names = new ArrayList<String>();


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.watch);     
 }
    public void setBoatsinWatch() {

        //setContentView(R.layout.watch);

        LinearLayout ll = (LinearLayout) findViewById(R.id.boats);
        ll.removeAllViews();

        int j;
        SettingsActivity s = new SettingsActivity();


        s.initBoatNames();
        boat_names = s.getBoats();

        for (j=1;j<boat_names.size();j++) {

            LinearLayout bl = new LinearLayout(this);
            bl.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,               LayoutParams.WRAP_CONTENT));
            bl.setOrientation(LinearLayout.HORIZONTAL);

           Button b = new Button(this);
           b.setText(boat_names.get(j));
           b.setWidth(200);
           b.setPadding(10, 10, 10, 10);

           TextView tv = new TextView(this);
           tv.setTextSize(20);
           tv.setText("0");
           tv.setTextColor(Color.BLACK);
           tv.setPadding(50, 0, 50, 0);

           ImageButton ib = new ImageButton(this);
           ib.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
           ib.setBackgroundResource(R.drawable.finish);

           bl.addView(b);
           bl.addView(tv);
           bl.addView(ib);
           ll.addView(bl);
        }

    }

Watch.xml

    <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
    android:background="#FFFFFF" android:weightSum="2">
    <TextView android:id="@+id/textView1" android:text=""         android:layout_width="wrap_content" android:layout_height="30dp"></TextView>  
    <LinearLayout android:layout_width="wrap_content"     android:layout_height="wrap_content" android:id="@+id/boats"     android:orientation="vertical">    
     </LinearLayout>
 </LinearLayout>

谢谢! 彼得

3 个答案:

答案 0 :(得分:1)

您应该启动WatchActivity而不是实例化

    public void onTabChanged(String tabId) {
        startActivity(new Intent(HelloAndroidActivity.this, WatchActivity.class)
        }


});

然后,您应该从setBoatsInWatch()致电WatchActivity.onCreate()

根据以下评论,这个答案解决了当前的问题。

答案 1 :(得分:1)

我找到了一个更简单的解决方案!

只需为每个标签添加:

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

答案 2 :(得分:0)

您的WatchActivity尚未布局,因此findViewById的空指针异常。

在调用findViewById之前,需要调用setContentView。我看到你在那里注释了它,除非它在其他地方被称为尚未发布的地方吗?