抱歉我的英文。我有TabActivity,我想创建自定义操作栏,但有这个错误:
04-25 20:11:17.777:E / AndroidRuntime(8083): java.lang.RuntimeException:无法启动活动 ComponentInfo {com.example.bonsitelazyl / com.example.bonsitelazyl.MainActivity}: java.lang.RuntimeException:无法启动活动 ComponentInfo {com.example.bonsitelazyl / com.example.bonsitelazyl.News}: 显示java.lang.NullPointerException
MainActivity:
public class MainActivity extends TabActivity {
TabHost tabHost;
ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = getTabHost();
setTabs();
}
private void setTabs()
{
addTab("", R.drawable.tab_news, News.class);
addTab("", R.drawable.tab_servises, News.class);
addTab("", R.drawable.tab_profile, News.class);
addTab("", R.drawable.tab_contacts, News.class);
}
private void addTab(String labelId, int drawableId, Class<?> c)
{
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
}
新闻
public class News extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_n);
ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
}
}
UPD
所有日志猫
04-25 20:33:15.186:E / AndroidRuntime(12242):致命异常:主要 04-25 20:33:15.186:E / AndroidRuntime(12242): java.lang.RuntimeException:无法启动活动 ComponentInfo {com.example.bonsitelazyl / com.example.bonsitelazyl.MainActivity}: java.lang.RuntimeException:无法启动活动 ComponentInfo {com.example.bonsitelazyl / com.example.bonsitelazyl.News}: java.lang.NullPointerException 04-25 20:33:15.186: E / AndroidRuntime(12242):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2077) 04-25 20:33:15.186:E / AndroidRuntime(12242):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104) 04-25 20:33:15.186:E / AndroidRuntime(12242):at android.app.ActivityThread.access $ 600(ActivityThread.java:134)04-25 20:33:15.186:E / AndroidRuntime(12242):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1247) 04-25 20:33:15.186:E / AndroidRuntime(12242):at android.os.Handler.dispatchMessage(Handler.java:99)04-25 20:33:15.186:E / AndroidRuntime(12242):at android.os.Looper.loop(Looper.java:154)04-25 20:33:15.186: E / AndroidRuntime(12242):at android.app.ActivityThread.main(ActivityThread.java:4624)04-25 20:33:15.186:E / AndroidRuntime(12242):at java.lang.reflect.Method.invokeNative(Native Method)04-25 20:33:15.186:E / AndroidRuntime(12242):at java.lang.reflect.Method.invoke(Method.java:511)04-25 20:33:15.186: E / AndroidRuntime(12242):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:809) 04-25 20:33:15.186:E / AndroidRuntime(12242):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)04-25 20:33:15.186:E / AndroidRuntime(12242):at dalvik.system.NativeStart.main(Native Method)04-25 20:33:15.186: E / AndroidRuntime(12242):引起:java.lang.RuntimeException:无效 开始活动 ComponentInfo {com.example.bonsitelazyl / com.example.bonsitelazyl.News}: java.lang.NullPointerException 04-25 20:33:15.186: E / AndroidRuntime(12242):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2077) 04-25 20:33:15.186:E / AndroidRuntime(12242):at android.app.ActivityThread.startActivityNow(ActivityThread.java:1917) 04-25 20:33:15.186:E / AndroidRuntime(12242):at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135) 04-25 20:33:15.186:E / AndroidRuntime(12242):at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347) 04-25 20:33:15.186:E / AndroidRuntime(12242):at android.widget.TabHost $ IntentContentStrategy.getContentView(TabHost.java:682) 04-25 20:33:15.186:E / AndroidRuntime(12242):at android.widget.TabHost.setCurrentTab(TabHost.java:346)04-25 20:33:15.186:E / AndroidRuntime(12242):at android.widget.TabHost.addTab(TabHost.java:236)04-25 20:33:15.186: E / AndroidRuntime(12242):at com.example.bonsitelazyl.MainActivity.addTab(MainActivity.java:47) 04-25 20:33:15.186:E / AndroidRuntime(12242):at com.example.bonsitelazyl.MainActivity.setTabs(MainActivity.java:29) 04-25 20:33:15.186:E / AndroidRuntime(12242):at com.example.bonsitelazyl.MainActivity.onCreate(MainActivity.java:24) 04-25 20:33:15.186:E / AndroidRuntime(12242):at android.app.Activity.performCreate(Activity.java:4479)04-25 20:33:15.186:E / AndroidRuntime(12242):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050) 04-25 20:33:15.186:E / AndroidRuntime(12242):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041) 04-25 20:33:15.186:E / AndroidRuntime(12242):... 11更多04-25 20:33:15.186:E / AndroidRuntime(12242):引起: java.lang.NullPointerException 04-25 20:33:15.186: E / AndroidRuntime(12242):at com.example.bonsitelazyl.News.onCreate(News.java:15)04-25 20:33:15.186:E / AndroidRuntime(12242):at android.app.Activity.performCreate(Activity.java:4479)04-25 20:33:15.186:E / AndroidRuntime(12242):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050) 04-25 20:33:15.186:E / AndroidRuntime(12242):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041)
UPD:
getActionBar 返回null
UPD:
我在新的i更新中尝试android.support.v7.app.ActionBarActivity,我添加此代码:
getSupportActionBar().setDisplayShowCustomEnabled(true);
View customNav = LayoutInflater.from(this).inflate(R.layout.custom_actionbar, null);
getSupportActionBar().setCustomView(customNav);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setCustomView(customNav);
04-25 21:09:09.550:E / AndroidRuntime(22075):java.lang.RuntimeException:无法启动活动 ComponentInfo {com.example.bonsitelazyl / com.example.bonsitelazyl.MainActivity}: java.lang.RuntimeException:无法启动活动 ComponentInfo {com.example.bonsitelazyl / com.example.bonsitelazyl.News}: 显示java.lang.NullPointerException
答案 0 :(得分:0)
TabActivity是不推荐使用的类。要为您的班级添加标签,请使用上面的代码。
MainActivity2Activity.java
import java.util.Locale;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity2Activity extends ActionBarActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_activity2, container, false);
return rootView;
}
}
}
布局/ activity_main_activity2.xml
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="georgeoikonomou.fishinggps.MainActivity2Activity"/>
布局/ fragment_main_activity2.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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="georgeoikonomou.fishinggps.MainActivity2Activity$PlaceholderFragment">
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
答案 1 :(得分:0)
由于@Giorgos Oikonomou已提及TabActivity
已被弃用的课程,因此建议您使用this方式在应用中实施制表符。
自定义actionBar
存在一些问题。
使用自定义ActionBar
的方式如下所述:
1 - 创建Actionbar
的自定义布局。
2 - 在OnCreate
方法中添加以下行:
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.action_bar);
(如果您未使用支持库,请将此更改为getSupportActionBar()
:getActionBar()
)
现在,要获取actionBar
中的项目,请执行以下操作:
(我假设您在名为TextView
的操作栏action_bar_title
中使用了标题
TextView tv = (TextView) getSupportActionBar().getCustomView().findViewById(R.id.action_bar_title);