我的项目崩溃并显示从活动切换到片段时出现以下错误。
08-12 20:00:56.835 1711-1711/com.facebooklogin E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at info.fastbadmainton2.slidingmenu.NewsFragment.onCreateView(NewsFragment.java:66)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
at android.app.BackStackRecord.run(BackStackRecord.java:635)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
at dalvik.system.NativeStart.main(Native Method)
它在声明中显示错误 -
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
为什么它在这个语句中显示错误?我的应用程序没有崩溃,如果我删除这个语句但我的片段变得透明,这样活动可见后面。为什么我的片段becoms透明?
活动
public class NewsPopup extends FragmentActivity {
TextView tv1,tv2,tv3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newspopup);
Configuration config = getResources().getConfiguration();
FragmentManager fragmentManager = getFragmentManager();
final FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
tv1=(TextView)findViewById(R.id.textView21);
tv1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment newFragment = new NewsFragment();
fragmentTransaction.replace(android.R.id.content, newFragment);
fragmentTransaction.commit();
}
});
tv2=(TextView)findViewById(R.id.textView22);
tv2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent();
i.setClass(getApplicationContext(), NewsPopup2.class);
startActivity(i);
}
});
tv3=(TextView)findViewById(R.id.textView24);
tv3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent();
i.setClass(getApplicationContext(), NewsPopup3.class);
startActivity(i);
}
});
}
}
片段
public class NewsFragment extends Fragment {
private FragmentActivity myContext;
private ViewPager viewPager;
private TabsPagerAdapter2 mAdapter;
ActionBar actionBar;
private String[] tabs = { "My Teams", "All Teams", "Trending" };
public NewsFragment(){}
@Override
public void onAttach(Activity activity) {
myContext=(FragmentActivity) activity;
super.onAttach(activity);
}
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.news, container, false);
actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
TextView tv=(TextView)rootView.findViewById(R.id.textView11);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent();
i.setClass(myContext, NewsPopup.class);
startActivity(i);
}
});
TextView tv1=(TextView)rootView.findViewById(R.id.textView13);
tv1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent();
i.setClass(myContext, NewsPopup2.class);
startActivity(i);
}
});
TextView tv2=(TextView)rootView.findViewById(R.id.textView43);
tv2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent();
i.setClass(myContext, NewsPopup3.class);
startActivity(i);
}
});
return rootView;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_search2:
// recordVideo();
//countClick++;
Intent i = new Intent();
i.setClass(myContext, LeagueSelect2.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/*@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}*/
}
答案 0 :(得分:0)
您可能正在使用支持片段。但是您的活动扩展了FragmentActivity。 getActionBar()在旧设备上返回null,因为它们没有任何操作栏。让您的活动从appcompat库扩展ActionBarActivity,然后调用
actionBar = ((ActionBarActivity) getActivity()).getActionBar();