Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 天文钟不';最初添加到布局时不绘制_Android_Android Layout_Android Tabhost_Tabwidget_Chronometer - Fatal编程技术网

Android 天文钟不';最初添加到布局时不绘制

Android 天文钟不';最初添加到布局时不绘制,android,android-layout,android-tabhost,tabwidget,chronometer,Android,Android Layout,Android Tabhost,Tabwidget,Chronometer,我正在尝试编写一个简单的计时器应用程序。我正在创建一个计数器类来扩展计时器。无论如何,当我在onCreate()中将自定义计时器添加到布局时,它不会被绘制。只有当我切换到另一个选项卡,然后返回到“计数器选项卡”时,它才会被绘制出来。之后它会保持不变。基本上,我怎么画呢?我尝试调用invalidate(),但似乎没有任何效果 下面是有问题的代码: 这在我的onCreate()中。我在一个叫做linLay的线性布局中添加了定制的计时器 public class TimerMain exten

我正在尝试编写一个简单的计时器应用程序。我正在创建一个计数器类来扩展计时器。无论如何,当我在onCreate()中将自定义计时器添加到布局时,它不会被绘制。只有当我切换到另一个选项卡,然后返回到“计数器选项卡”时,它才会被绘制出来。之后它会保持不变。基本上,我怎么画呢?我尝试调用invalidate(),但似乎没有任何效果

下面是有问题的代码:

这在我的onCreate()中。我在一个叫做linLay的线性布局中添加了定制的计时器

    public class TimerMain extends Activity {

    // Handle to the tabhost
    public TabHost tabHost;

    // Handle to the tab widget (this is the view that has the two tabs)
    public TabWidget tabWidget;

    // Handle to the LinearLayout inside the ScrollView
    public LinearLayout linLay;

    // Spec used to create the tabs
    public TabSpec spec;

    // Up Counter handle
    public UpCounter myUpCounter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the view to the main layout (see main.xml)
        setContentView(R.layout.main);

        // Get the handle to the tabhost in the layout and set it up
        tabHost = (TabHost) this.findViewById(R.id.my_tabhost);
        tabHost.setup();

        // Get the handle to the TabWidget that is in the 
        // xml file through the TabHost handle
        tabWidget = tabHost.getTabWidget();

        // Get the handle to the relative layout that is inside of the frame
        // that is used for the tab contents
        linLay = (LinearLayout) this.findViewById(R.id.LinLay);

        // Create the Count Up tab and leave it blank
        spec = tabHost.newTabSpec("CU");
        spec.setIndicator("Count Up");
        spec.setContent(R.id.ScrLay);
        tabHost.addTab(spec);

        // Create the Count Down tab and leave it blank
        spec = tabHost.newTabSpec("CD");
        spec.setIndicator("Count Down");
        spec.setContent(R.id.ScrLay);
        tabHost.addTab(spec);

        // Set up the click listeners for each tab
        initTabClickListen();

        // Start with the Count Up tab selected
        tabHost.setCurrentTab(0);

        // Initialize the first counters
        myUpCounter = initFirstCounters(this.getApplicationContext());        
        myUpCounter.start();
        myUpCounter.setBase(SystemClock.elapsedRealtime());
        //Add the first UpCounter to the scroll view
        linLay.addView(myUpCounter);

    }

    @Override
    protected void onResume(){
        super.onResume();
    }

    private UpCounter initFirstCounters(Context context) {
        UpCounter tempCt;
        // Create the first counter
        tempCt = new UpCounter(context);
        tempCt.setFormat("Initial Format: %s");
        return tempCt;

    }

    // Set up the click listeners for each tab
    private void initTabClickListen() {

        // Set up the listener for the Count Up Tab
        tabWidget.getChildAt(0).setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                // Detach all views from the layout
                // and add the views for this tab.
                linLay.removeAllViews();
                linLay.addView(myUpCounter);


                // Set the current tab to Count Up
                tabHost.setCurrentTab(0);
            }
        });

        // Set up the listener for the Count Down Tab
        tabWidget.getChildAt(1).setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                // Detach all views from the layout
                // and add the views for this tab.
                linLay.removeAllViews();
                //scrLay.addView(v2);

                // Set the current tab to Count Down
                tabHost.setCurrentTab(1);
            }
        });

    }
}

// Custom Counter Class
class UpCounter extends Chronometer{

    public UpCounter(Context context) {
        super(context);

    }

}
最后,这里是.xml,以防您想看到它。我也尝试过移除线性布局,使用scrollview来代替计时器,我也有同样的问题

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
         <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <ScrollView 
                android:id="@+id/ScrLay"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

                <LinearLayout 
                    android:id="@+id/LinLay"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                </LinearLayout>

             </ScrollView>

        </FrameLayout>
    </LinearLayout>
</TabHost>

如何使此计数器在应用程序启动时显示?

请参见此

在这里,他们学习了如何制作基于选项卡的应用程序

现在,创建活动并将其设置为tabhost。并在那个活动上创建天文钟

如果没有解决,请告诉我。

我已经解决了

出于某种原因,如果我只是将其添加到onCreate()中的代码中:


现在一切正常

您以前尝试过任何基于选项卡的应用程序吗?请看一些演示,创建基于标签的应用程序,然后尝试将计时器放入该应用程序。不,这是我第一次玩标签。我在android文档中看到的演示使用了TabActivity,这是不推荐使用的。这个应用程序目前是针对Froyo的,所以除了我在这里所做的之外,我看不到任何其他制作标签应用程序的方法。所有Android示例都使用片段(用于API 11)或不推荐使用的TabActivity。1) 此示例扩展了不推荐使用的TabActivity。我不想使用TabActivity。2) 即使TabActivity没有被弃用,我也不想对每个选项卡使用单独的活动,我更希望在更改选项卡时交换视图。这将允许我保留在任一选项卡中创建的所有对象,并继续在后台处理它们。也许我可以为每个选项卡创建整个活动。只要这些活动是从创建tabhost的活动扩展而来的。我应该能够访问两个活动之间的现有对象。不知道也许值得一试。我得试着把它寄回来。
// Start with the Count Up tab selected
tabHost.setCurrentTab(1);
tabHost.setCurrentTab(0);