【问题标题】:android : error getting tabview app crashandroid:获取tabview应用程序崩溃时出错
【发布时间】:2014-04-11 08:53:41
【问题描述】:

我有一个 android 应用程序,我从以前的活动中获取一个字符串并将其转移到下一个活动。 当我使用一个标签时工作正常。但是当我为第二个标签指定它时,应用程序崩溃了

log cat 中的错误,您必须指定一种创建选项卡指示器的方法。

代码已解决

 package com.example.pms;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;






public class TabControl extends TabActivity 
{
    public static TabControl mTabControl;
    public static TextView textView;
    public static TabHost tabHost ;
    final Context context = this;
    //public static String strEmployeeID = "";
    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        Resources resources = getResources(); 
        TabHost tabHost = getTabHost(); 




          Bundle extras = getIntent().getExtras();
          String strEmployeeID="";
         TabSpec photospec = tabHost.newTabSpec("Hourly entry");
            // setting Title and Icon for the Tab
            photospec.setIndicator("Hourly Entry");

              if (extras != null)
              {

                  String value = extras.getString("new_variable_name");
//                Toast.makeText(getBaseContext(),  value, Toast.LENGTH_LONG).show();
                  strEmployeeID = value;
              }


             Intent photosIntent = new Intent(getApplicationContext(), HourlyEntry.class);
             photosIntent.putExtra("new_variable_name",strEmployeeID);
             photospec.setContent(photosIntent);



             TabSpec photospec1 = tabHost.newTabSpec("Leave app");
                // setting Title and Icon for the Tab
                photospec1.setIndicator("Leave App");

                  if (extras != null)
                  {

                      String value = extras.getString("new_variable_name");
//                    Toast.makeText(getBaseContext(),  value, Toast.LENGTH_LONG).show();
                      strEmployeeID = value;
                  }


                 Intent photosIntent1 = new Intent(getApplicationContext(), LeaveApp.class);
                 photosIntent1.putExtra("new_variable_name",strEmployeeID);
                 photospec1.setContent(photosIntent1);


        tabHost.addTab(photospec);
        tabHost.addTab(photospec1);



        tabHost.setCurrentTab(0);

    }


}

【问题讨论】:

  • 您是否将 LeaveApp、HourlyEntry 添加到 Manifiest?
  • 然后发布您的 logcat。
  • 是的...我只会发布我的日志猫
  • 张贴我的日志请看
  • 这里你的两个选项卡名称都是一样的 TabSpec photospec1 = tabHost.newTabSpec("Photos");TabSpec photospec = tabHost.newTabSpec("Photos"); 更改名称并尝试

标签: android android-intent tabs android-tabhost


【解决方案1】:

改变这个

    TabSpec photospec1 = tabHost.newTabSpec("Photos");
      // setting Title and Icon for the Tab
    photospec.setIndicator("", getResources().getDrawable(R.drawable.tab_home));

     TabSpec photospec1 = tabHost.newTabSpec("Photos2");
            // setting Title and Icon for the Tab
     photospec1.setIndicator("", getResources().getDrawable(R.drawable.tab_home));
      if (extras != null)

              {
                  String value = extras.getString("new_variable_name");
                  strEmployeeID = value;
              }

         Intent photosIntent1 = new Intent(getApplicationContext(), LeaveApp.class);
         photosIntent1.putExtra("new_variable_name",strEmployeeID);
         photospec1.setContent(photosIntent1);

而您的问题就在这里,您从未将Tab Indicator 设置为第二个标签正确,如下所示

  photospec1.setIndicator("", getResources().getDrawable(R.drawable.tab_home));

而且你的问题是你在这里设置了错误的set Content Intent到第二个标签正确,如下所示:

    Intent photosIntent1 = new Intent(getApplicationContext(), LeaveApp.class);
    photosIntent1.putExtra("new_variable_name",strEmployeeID);
    photospec1.setContent(photosIntent1);

【讨论】:

    猜你喜欢
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 2012-10-04
    • 1970-01-01
    相关资源
    最近更新 更多