android TabHost 对象报错

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TabHost.setup()' on a null object reference
        at com.example.ceshi.MainActivity.onCreate(MainActivity.java:31)

 

没有解决

 

MainActivity:

package com.example.ceshi;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.GridView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.SeekBar;
import android.widget.SimpleAdapter;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TabHost t = (TabHost) findViewById(android.R.id.tabhost);
        t.setup();
        LayoutInflater i = LayoutInflater.from(this);
        i.inflate(R.layout.tab1,t.getTabContentView());
        i.inflate(R.layout.tab2,t.getTabContentView());
        t.addTab(t.newTabSpec("tab1").setIndicator("竞选表情").setContent(R.id.left));
        t.addTab(t.newTabSpec("tab2").setIndicator("爱你").setContent(R.id.right));
    }
}

 

布局文件:



  
    
    
    
    
  

你可能感兴趣的:(android,开发)