freedom_wings 2015-12-13 14:44 采纳率: 21.1%
浏览 1427

安卓,fragmentTabHost中,使用自定义的view后就出错了,求指导

原本在tabspec里的setIndicator就使用简单的字符串,没事,已经完成了。

然后想试试这里使用自定义样式,就报了个nullpointerException

下面贴下代码:

package com.example.test_fragmenttaghost;

import android.os.Bundle;
import android.view.*;
import com.example.test_fragmenttaghost.Fragment1;
import com.example.test_fragmenttaghost.Fragment2;
import com.example.test_fragmenttaghost.Fragment3;
import android.widget.*;
import android.app.Activity;
import android.view.Menu;
import android.support.v4.app.*;
import android.widget.TabHost.*;

public class MainActivity extends FragmentActivity {
private FragmentTabHost tabhost;
Class[] fragmentclass={Fragment1.class,Fragment2.class,Fragment3.class};
String[] str={"标签1","标签2","标签3"};
private LayoutInflater inflater;
int[] pictureId={R.drawable.a1,R.drawable.a2,R.drawable.a3};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inflater=LayoutInflater.from(this);
tabhost=(FragmentTabHost)super.findViewById(R.id.fragmenttabhost);
tabhost.setup(this,getSupportFragmentManager(),R.id.tabcontent);
TabSpec tabspec;
int i;
for(i=0;i<3;i++)
{
tabspec=tabhost.newTabSpec("f"+i);
tabspec.setIndicator(getItemView(i));
tabhost.addTab(tabspec,fragmentclass[i],null);
tabhost.getTabWidget().getChildAt(i);
}

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public View getItemView(int index)
{

    View v=inflater.inflate(R.layout.picture,null);
    ImageView img=(ImageView)super.findViewById(R.id.picture1);
    img.setImageResource(pictureId[index]);
    TextView tv=(TextView)super.findViewById(R.id.textview);
    tv.setText(str[index]);

    return v;
}

}

这里比原来的代码新增加的就getItemView那段,还有layoutInflater对象的声明和实例化,还有新增加了一个用于自定义样式的xml文件(即getItemView里的R.layout.picture),最多还有原来的setIndicator(str[i])改成了现在的setIndicator(getItemView(i));

错误应该源自这几个地方,但我怎么都想不出哪里null了,求指教。

  • 写回答

2条回答 默认 最新

  • devmiao 2015-12-13 14:53
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)