我正在尝试使用标签设置自定义ActionBar,但它显示错误显示.plz检查它&帮助我。
没有ActionBar正常工作。
...错误
04-26 11:25:20.615: D/AndroidRuntime(7831): Shutting down VM
04-26 11:25:20.616: W/dalvikvm(7831): threadid=1: thread exiting with uncaught exception (group=0x41ddfd40)
04-26 11:25:20.619: E/AndroidRuntime(7831): FATAL EXCEPTION: main
04-26 11:25:20.619: E/AndroidRuntime(7831): Process: com.android.zerocool.tabviewwithactonbar, PID: 7831
04-26 11:25:20.619: E/AndroidRuntime(7831): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.zerocool.tabviewwithactonbar/com.android.zerocool.tabviewwithactonbar.MainActivity}: java.lang.NullPointerException
04-26 11:25:20.619: E/AndroidRuntime(7831): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
04-26 11:25:20.619: E/AndroidRuntime(7831): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
04-26 11:25:20.619: E/AndroidRuntime(7831): at android.app.ActivityThread.access$800(ActivityThread.java:139)
04-26 11:25:20.619: E/AndroidRuntime(7831): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
04-26 11:25:20.619: E/AndroidRuntime(7831): at android.os.Handler.dispatchMessage(Handler.java:102)
04-26 11:25:20.619: E/AndroidRuntime(7831): at android.os.Looper.loop(Looper.java:136)
04-26 11:25:20.619: E/AndroidRuntime(7831): at android.app.ActivityThread.main(ActivityThread.java:5086)
04-26 11:25:20.619: E/AndroidRuntime(7831): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 11:25:20.619: E/AndroidRuntime(7831): at java.lang.reflect.Method.invoke(Method.java:515)
04-26 11:25:20.619: E/AndroidRuntime(7831): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
04-26 11:25:20.619: E/AndroidRuntime(7831): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
04-26 11:25:20.619: E/AndroidRuntime(7831): at dalvik.system.NativeStart.main(Native Method)
04-26 11:25:20.619: E/AndroidRuntime(7831): Caused by: java.lang.NullPointerException
04-26 11:25:20.619: E/AndroidRuntime(7831): at com.android.zerocool.tabviewwithactonbar.Global.setActionbar(Global.java:16)
04-26 11:25:20.619: E/AndroidRuntime(7831): at com.android.zerocool.tabviewwithactonbar.MainActivity.onCreate(MainActivity.java:24)
04-26 11:25:20.619: E/AndroidRuntime(7831): at android.app.Activity.performCreate(Activity.java:5248)
04-26 11:25:20.619: E/AndroidRuntime(7831): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
04-26 11:25:20.619: E/AndroidRuntime(7831): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
04-26 11:25:20.619: E/AndroidRuntime(7831): ... 11 more
第二个文件是Global.java我在这个文件中制作自定义栏
public class Global {
public static void setActionbar(Context context, ActionBar ActionBar,
String title) {
ActionBar.setDisplayShowHomeEnabled(false);
ActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater Inflater = LayoutInflater.from(context);
View CustomView = Inflater.inflate(R.layout.custom_actionbar, null);
TextView TitleTextView = (TextView) CustomView
.findViewById(R.id.title_text);
TitleTextView.setText(title);
ActionBar.setCustomView(CustomView);
ActionBar.setDisplayShowCustomEnabled(true);
}
MainActivity.java就在这里
public class MainActivity extends TabActivity implements OnTabChangeListener,
OnClickListener {
private TabHost tHost;
ImageView iv1, iv2, iv3, iv4, iv5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Global.setActionbar(this, getActionBar(), getString(R.string.first_tab));
initialiseTabHost();
}
public void onTabChanged(String tag) {
this.tHost.getCurrentTab();
// this.mViewPager.setCurrentItem(pos);
setSelectedTabColor();
}
private void setSelectedTabColor() {
switch (tHost.getCurrentTab()) {
case 0:
// Global.setActionbar(this, getActionBar(),
// getString(R.string.first_tab));
iv1.setImageResource(R.drawable.btn_deals_hover);
iv2.setImageResource(R.drawable.btn_subscription);
iv3.setImageResource(R.drawable.btn_post_deal);
iv4.setImageResource(R.drawable.btn_profile);
iv5.setImageResource(R.drawable.btn_setting);
break;
case 1:
iv1.setImageResource(R.drawable.btn_deals);
iv2.setImageResource(R.drawable.btn_subscription_hover);
iv3.setImageResource(R.drawable.btn_post_deal);
iv4.setImageResource(R.drawable.btn_profile);
iv5.setImageResource(R.drawable.btn_setting);
break;
case 2:
iv1.setImageResource(R.drawable.btn_deals);
iv2.setImageResource(R.drawable.btn_subscription);
iv3.setImageResource(R.drawable.btn_post_deal_hover);
iv4.setImageResource(R.drawable.btn_profile);
iv5.setImageResource(R.drawable.btn_setting);
break;
case 3:
iv1.setImageResource(R.drawable.btn_deals);
iv2.setImageResource(R.drawable.btn_subscription);
iv3.setImageResource(R.drawable.btn_post_deal);
iv4.setImageResource(R.drawable.btn_profile_hover);
iv5.setImageResource(R.drawable.btn_setting);
break;
case 4:
iv1.setImageResource(R.drawable.btn_deals);
iv2.setImageResource(R.drawable.btn_subscription);
iv3.setImageResource(R.drawable.btn_post_deal);
iv4.setImageResource(R.drawable.btn_profile);
iv5.setImageResource(R.drawable.btn_setting_hover);
break;
default:
break;
}
}
// Tabs Creation
private void initialiseTabHost() {
iv1 = (ImageView) findViewById(R.id.iv1);
iv2 = (ImageView) findViewById(R.id.iv2);
iv3 = (ImageView) findViewById(R.id.iv3);
iv4 = (ImageView) findViewById(R.id.iv4);
iv5 = (ImageView) findViewById(R.id.iv5);
iv1.setOnClickListener(this);
iv2.setOnClickListener(this);
iv3.setOnClickListener(this);
iv4.setOnClickListener(this);
iv5.setOnClickListener(this);
tHost = getTabHost();
tHost.setup();
TabHost.TabSpec tSpec;
Intent ForwardIntent = new Intent(getApplicationContext(),
FirstTabActivity.class);
tSpec = tHost.newTabSpec("Tab1").setIndicator("")
.setContent(ForwardIntent);
tHost.addTab(tSpec);
tSpec = tHost
.newTabSpec("Tab2")
.setIndicator("")
.setContent(
new Intent().setClass(this, SecondTabActivity.class));
tHost.addTab(tSpec);
tSpec = tHost
.newTabSpec("Tab3")
.setIndicator("")
.setContent(new Intent().setClass(this, ThirdTabActivity.class));
tHost.addTab(tSpec);
tSpec = tHost
.newTabSpec("Tab4")
.setIndicator("")
.setContent(
new Intent().setClass(this, FourthTabActivity.class));
tHost.addTab(tSpec);
iv5.setVisibility(View.VISIBLE);
tSpec = tHost
.newTabSpec("Tab5")
.setIndicator("")
.setContent(new Intent().setClass(this, FifthTabActivity.class));
tHost.addTab(tSpec);
tHost.setOnTabChangedListener(this);
tHost.getTabWidget().getChildAt(0)
.setBackgroundResource(R.drawable.btn_deals);
tHost.getTabWidget().getChildAt(1)
.setBackgroundResource(R.drawable.btn_business);
tHost.getTabWidget().getChildAt(2)
.setBackgroundResource(R.drawable.btn_profile);
tHost.getTabWidget().getChildAt(3)
.setBackgroundResource(R.drawable.btn_setting);
tHost.setCurrentTab(0);
setSelectedTabColor();
}
@Override
public void onClick(View v) {
if (v == iv1) {
tHost.setCurrentTab(0);
} else if (v == iv2) {
tHost.setCurrentTab(1);
} else if (v == iv3) {
tHost.setCurrentTab(2);
} else if (v == iv4) {
tHost.setCurrentTab(3);
} else if (v == iv5) {
tHost.setCurrentTab(4);
}
setSelectedTabColor();
}
}
我的XMl主要活动文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<LinearLayout
android:id="@+id/tabbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv_Home"
android:layout_width="match_parent"
android:layout_height="48dip"
android:layout_weight="1"
android:contentDescription="@null"
android:src="@drawable/btn_home" />
<ImageView
android:id="@+id/iv_Chat"
android:layout_width="match_parent"
android:layout_height="48dip"
android:layout_weight="1"
android:contentDescription="@null"
android:src="@drawable/btn_chat" />
<ImageView
android:id="@+id/iv_Post"
android:layout_width="match_parent"
android:layout_height="48dip"
android:layout_weight="1"
android:contentDescription="@null"
android:src="@drawable/btn_post_hover" />
<ImageView
android:id="@+id/iv_Alert"
android:layout_width="match_parent"
android:layout_height="48dip"
android:layout_weight="1"
android:contentDescription="@null"
android:src="@drawable/btn_alert" />
<ImageView
android:id="@+id/iv_More"
android:layout_width="match_parent"
android:layout_height="48dip"
android:layout_weight="1"
android:contentDescription="@null"
android:src="@drawable/btn_more" />
</LinearLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="horizontal"
android:showDividers="none"
android:tabStripEnabled="false"
android:visibility="gone" />
</LinearLayout>
</TabHost>
和cutome动作栏低于......
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/app_color" >
<ImageView
android:id="@+id/iv_logo"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_centerVertical="true"
android:layout_marginLeft="10dip"
android:src="@drawable/title_logo" />
<TextView
android:id="@+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="15dp"
android:textColor="@color/white"
android:layout_toRightOf="@+id/iv_logo"
android:text="this is textview"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>