Problem running android HelloTabWidget example - NullPointerException on addTab()

后端 未结 12 1217
陌清茗
陌清茗 2021-01-13 17:17

I\'ve tried the Tab Layout example, and I\'ve also fixed the few typos in the example (and added all the activities to the manifest). However, when I run it on the emulator

12条回答
  •  感动是毒
    2021-01-13 17:44

    Hey do the following steps and i am sure that your problem goes out:-

    Create a class HelloTabWidget.java

    package com.pericent;             //this is package name
    import android.app.TabActivity;
    import android.content.Intent;
    import android.content.res.Resources;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.TabHost;
    
    public class HelloTabWidget extends TabActivity  {
    
    private String TAG="HelloTabWidget";
    
    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,ArtistsActivity.class);
      Log.v(TAG,"---artist activity is called---");
    
      // Initialize a TabSpec for each tab and add it to the TabHost
      spec = tabHost.newTabSpec("artists").setIndicator("Artists",res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent);
      tabHost.addTab(spec);
    
    
      // Do the same for the other tabs
      intent = new Intent().setClass(this,AlbumsActivity.class);
      Log.v(TAG,"---album activity is called---");
      spec = tabHost.newTabSpec("albums").setIndicator("Albums",res.getDrawable(R.drawable.ic_tab_albums)).setContent(intent);
      tabHost.addTab(spec);
    
      intent = new Intent().setClass(this, SongsActivity.class);
      Log.v(TAG,"---song activity is called---");
      spec = tabHost.newTabSpec("songs").setIndicator("Songs",res.getDrawable(R.drawable.ic_tab_songs)).setContent(intent);
      tabHost.addTab(spec);
    
      }
    
    }
    

    Create your second activity: ArtistActivity.java

    package com.pericent;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.TextView;
    
    public class ArtistsActivity extends Activity {
     private String TAG="ArtistsActivity";
        /** Called when the activity is first created. */
        @Override
          public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            TextView textview=new TextView(this);
            textview.setText("This is Artist Activity");
            setContentView(textview);
            Log.v(TAG,"---in artist activity---");
        }
    }
    

    Create your third activity: AlbumsActivity.java

    package com.pericent;
    
    import android.R;
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.TextView;
    
    public class AlbumsActivity extends Activity{
        private String TAG="AlbumsActivity";
        @Override
        public void onCreate(Bundle savedInstance)
        {
            super.onCreate(savedInstance);
            TextView textview_album=new TextView(this);
            textview_album.setText("This is album activity");
            setContentView(textview_album);
            Log.v(TAG,"---in album activity---");
        }
    
    }
    

    Create your fourth activity: SongsActivity.java

    package com.pericent;
    
    import android.R;
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.TextView;
    
    public class SongsActivity extends Activity{
        private String TAG="SongsActivity";
        @Override
        public void onCreate(Bundle savedInstance)
        {
            super.onCreate(savedInstance);
            TextView textview_song=new TextView(this);
            textview_song.setText("This is song activity");
            setContentView(textview_song);
            Log.v(TAG,"---in songs activity---");
        }
    
    }
    

    Make a folder in res/drawable In this folder make 3 XML files: the code of these files like this:-

    
    
        
        
        
        
    
    

    In the above XML code we use two images, the following are images which must be save in the same folder (res/drawable).

    enter image description here enter image description here

    This is the main.xml:

    
    
    
        
        
               
         
            
        
    
    
    

    This is AdroidManifest:

    
    
        
           
              
                    
                     
              
           
           
              
           
        
     
    

    I think this all information will help, if you have any problem then free to ask me anything. I am always feel happy to help anyone.

提交回复
热议问题