我正在处理一个片段活动,其中包含一些tabs.While运行代码,我得到null对象引用问题。下面给出了代码。
1。 ActivityProfessionalTimeline.java
public class Activity_ProfessionalTimeline extends FragmentActivity implements
ActionBar.TabListener, OnClickListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private TextView mtext_FirstTab;
private TextView mtext_SecondTab;
private TextView mtext_ThirdTab;
private TextView mtext_FourthTab;
private ImageView _img_Profile;
private TextView _txt_UserName;
private TextView _txt_UserDestination;
ArrayList<Model_LoginDetails> arr_LogInUserDetails = new ArrayList<Model_LoginDetails>();
private ImageView _imt_userImage;
private ListView _listView_SelectOpition;
private boolean profilebtnstatus = false;
private RelativeLayout _relativelayour_status;
private ListView _listviewmoroption;
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_professionalttimeline);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
Bundle bundle = getIntent().getExtras();
arr_LogInUserDetails = bundle.getParcelableArrayList("mylist");
_imt_userImage = (ImageView) findViewById(R.id.imt_userImage);
_txt_UserName = (TextView) findViewById(R.id.txt_UserName);
_txt_UserDestination = (TextView) findViewById(R.id.txt_Userdestination);
_img_Profile = (ImageView) findViewById(R.id.img_Profile);
_listviewmoroption = (ListView) findViewById(R.id.listviewmoroption);
_listviewmoroption.setAdapter(new Adapter_MenuOpition(
Activity_ProfessionalTimeline.this));
_listviewmoroption.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
switch (arg2) {
case 0:
fun_hidemenu();
Bundle bundle = new Bundle();
Intent int_ProfessionalTimeline = new Intent(
Activity_ProfessionalTimeline.this,
Acitivty_AccountSetting.class);
bundle.putParcelableArrayList("mylist",
arr_LogInUserDetails);
int_ProfessionalTimeline.putExtras(bundle);
startActivity(int_ProfessionalTimeline);
break;
case 1:
break;
case 2:
finish();
break;
default:
break;
}
}
});
_relativelayour_status = (RelativeLayout) findViewById(R.id.reltv_Menu);
_listView_SelectOpition = (ListView) findViewById(R.id.list_allSelection);
_listView_SelectOpition.setAdapter(new Adapter_SelectionOpition(
Activity_ProfessionalTimeline.this));
_listView_SelectOpition
.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
drawer.closeDrawers();
switch (arg2) {
case 0:
break;
case 1:
Intent int_Intership = new Intent(Activity_ProfessionalTimeline.this, Tab_InterShip.class);
int_Intership.putExtra("image", arr_LogInUserDetails.get(0).getThumbnail_url());
startActivity(int_Intership);
break;
case 2:
Intent int_AddJob = new Intent(Activity_ProfessionalTimeline.this, Tab_AddJob.class);
int_AddJob.putExtra("image", arr_LogInUserDetails.get(0).getThumbnail_url());
int_AddJob.putExtra("addjob", "addJob");
startActivity(int_AddJob);
break;
case 3:
Intent int_SearchJob = new Intent(Activity_ProfessionalTimeline.this, Tab_SearchJob.class);
int_SearchJob.putExtra("image", arr_LogInUserDetails.get(0).getThumbnail_url());
startActivity(int_SearchJob);
break;
case 4:
Intent int_PostedJob = new Intent(Activity_ProfessionalTimeline.this, Tab_PostedJob.class);
int_PostedJob.putExtra("image", arr_LogInUserDetails.get(0).getThumbnail_url());
startActivity(int_PostedJob);
break;
case 5:
Intent int_AppliedJob = new Intent(Activity_ProfessionalTimeline.this, TabFragment_AppliedJob.class);
int_AppliedJob.putExtra("image", arr_LogInUserDetails.get(0).getThumbnail_url());
startActivity(int_AppliedJob);
break;
case 6:
Intent int_JobMENTOR = new Intent(Activity_ProfessionalTimeline.this, Tab_JobMentor.class);
int_JobMENTOR.putExtra("image", arr_LogInUserDetails.get(0).getThumbnail_url());
startActivity(int_JobMENTOR);
break;
case 7:
Intent int_JobMEtees = new Intent(Activity_ProfessionalTimeline.this, Tab_mentees.class);
int_JobMEtees.putExtra("image", arr_LogInUserDetails.get(0).getThumbnail_url());
startActivity(int_JobMEtees);
break;
case 8:
break;
case 9:
break;
case 10:
break;
default:
break;
}
}
});
_img_Profile.setOnClickListener(this);
Picasso.with(Activity_ProfessionalTimeline.this)
.load(arr_LogInUserDetails.get(0).getThumbnail_url())
.into(_imt_userImage);
_txt_UserName.setText(arr_LogInUserDetails.get(0).getUser_Name());
_txt_UserDestination.setText(arr_LogInUserDetails.get(0)
.getUser_Skills());
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setOffscreenPageLimit(1);
LayoutInflater inflator = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
actionBar = getActionBar();
// actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
// actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setStackedBackgroundDrawable(new ColorDrawable(Color
.parseColor("#fbae38")));
actionBar.setBackgroundDrawable(new ColorDrawable(Color
.parseColor("#2b4d72")));
// actionBar.setIcon(R.drawable.appicon);
View actionBarView = inflator.inflate(R.layout.actionbar_custom_layout,
null);
ImageView _mProfileImage = (ImageView) actionBarView
.findViewById(R.id.img_Profile);
Picasso.with(Activity_ProfessionalTimeline.this)
.load(arr_LogInUserDetails.get(0).getThumbnail_url())
.into(_mProfileImage);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
getActionBar().setCustomView(actionBarView);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager(),
arr_LogInUserDetails, Activity_ProfessionalTimeline.this);
viewPager.setAdapter(mAdapter);
// Adding First Tabs
mtext_FirstTab = new TextView(Activity_ProfessionalTimeline.this);
mtext_FirstTab.setText("Social");
mtext_FirstTab.setCompoundDrawablePadding(15);
mtext_FirstTab.setTextColor(Color.parseColor("#ffffff"));
mtext_FirstTab.setBackgroundColor(Color.parseColor("#fbae38"));
mtext_FirstTab.setTextSize(12);
mtext_FirstTab.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.social, 0, 0, 0);
mtext_FirstTab.setLayoutParams(layoutParams);
mtext_FirstTab.setGravity(Gravity.CENTER);
final Tab firstTab = actionBar.newTab().setCustomView(mtext_FirstTab);
firstTab.setTabListener(this);
// Add Second Tab
mtext_SecondTab = new TextView(Activity_ProfessionalTimeline.this);
mtext_SecondTab.setText("Professional");
mtext_SecondTab.setCompoundDrawablePadding(5);
mtext_SecondTab.setTextColor(Color.parseColor("#ffffff"));
mtext_SecondTab.setBackgroundColor(Color.parseColor("#fbae38"));
mtext_SecondTab.setTextSize(12);
mtext_SecondTab.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.professional, 0, 0, 0);
mtext_SecondTab.setLayoutParams(layoutParams);
mtext_SecondTab.setGravity(Gravity.CENTER);
final Tab SecondTab = actionBar.newTab().setCustomView(mtext_SecondTab);
SecondTab.setTabListener(this);
// AddThirdTab
mtext_ThirdTab = new TextView(Activity_ProfessionalTimeline.this);
mtext_ThirdTab.setText("Shop");
mtext_ThirdTab.setCompoundDrawablePadding(5);
mtext_ThirdTab.setTextColor(Color.parseColor("#ffffff"));
mtext_ThirdTab.setBackgroundColor(Color.parseColor("#fbae38"));
mtext_ThirdTab.setTextSize(12);
mtext_ThirdTab.setCompoundDrawablesWithIntrinsicBounds(R.drawable.shop,
0, 0, 0);
mtext_ThirdTab.setLayoutParams(layoutParams);
mtext_ThirdTab.setGravity(Gravity.CENTER);
final Tab ThirdTab = actionBar.newTab().setCustomView(mtext_ThirdTab);
ThirdTab.setTabListener(this);
// AddFourthTab
mtext_FourthTab = new TextView(Activity_ProfessionalTimeline.this);
mtext_FourthTab.setText("Blog");
mtext_FourthTab.setCompoundDrawablePadding(5);
mtext_FourthTab.setTextColor(Color.parseColor("#ffffff"));
mtext_FourthTab.setBackgroundColor(Color.parseColor("#fbae38"));
mtext_FourthTab.setTextSize(12);
mtext_FourthTab.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.blog, 0, 0, 0);
mtext_FourthTab.setLayoutParams(layoutParams);
mtext_FourthTab.setGravity(Gravity.CENTER);
final Tab FourthTab = actionBar.newTab().setCustomView(mtext_FourthTab);
FourthTab.setTabListener(this);
Tab[] arr = {firstTab, SecondTab, ThirdTab, FourthTab};
for (int i = 0; i < arr.length; i++) {
actionBar.addTab(arr[i]);
}
// for (String tab_name : tabs) {
// actionBar.addTab(actionBar.newTab().setText(tab_name)
// .setTabListener(this));
// }
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
fun_hidemenu();
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
fun_hidemenu();
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.img_Profile:
if (profilebtnstatus == false) {
_relativelayour_status.setVisibility(View.VISIBLE);
_img_Profile.setBackgroundColor(Color.parseColor("#faad38"));
profilebtnstatus = true;
} else {
fun_hidemenu();
}
break;
default:
break;
}
}
private void fun_hidemenu() {
if (profilebtnstatus == true) {
_relativelayour_status.setVisibility(View.GONE);
_img_Profile.setBackgroundColor(Color.parseColor("#2b4d72"));
profilebtnstatus = false;
}
}
}
2.logcat
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayOptions(int)' on a null object reference
at com.sevenhorse.almabay.Activity_ProfessionalTimeline.onCreate(Activity_ProfessionalTimeline.java:210)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
请帮我解决这个问题。
答案 0 :(得分:0)
如果您要导入android.app.ActionBar
,则必须使用getActionBar()
,如果要导入android.support.v7.app.ActionBar
,则可以使用getSupportActionBar()
。