Search code examples
androidandroid-intenttabsandroid-tabhost

android : error getting tabview app crash


i have android app where i get a string from previous activity and transfer it to next activity. works fine when i do it with one tab . but when i specify it for the second tab the app crashes

error in log cat you must specify a way to create the tab indicator.

code Solved

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

    }


}

Solution

  • Change this

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

    With

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

    And your issue is here you never set Tab Indicator to Second tab correct like below

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

    And also your issue is here you set wrong set Content Intent to Second tab correct like below:

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