我是Android开发的初学者。我刚开始学习Android开发。现在我试图隐藏片段中的操作栏。但这是在抛出错误。它抛出null异常。
我的代码出了什么问题?
这是我的活动:
public class MainActivity extends ActionBarActivity {
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[3];
drawerItem[0] = new ObjectDrawerItem(R.drawable.icon_1, "Create");
drawerItem[1] = new ObjectDrawerItem(R.drawable.icon_2, "Read");
drawerItem[2] = new ObjectDrawerItem(R.drawable.icon_3, "Help");
DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.listview_item_row, drawerItem);
mDrawerList = (ListView)findViewById(R.id.left_drawer);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(),view.getTag().toString(),Toast.LENGTH_SHORT).show();
selectItem(position);
}
}
private void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = null;//fragment for position zero
// Toast.makeText(getBaseContext(),"position 0",Toast.LENGTH_LONG).show();
break;
case 1:
fragment = null;
// Toast.makeText(getBaseContext(),"position 1",Toast.LENGTH_LONG).show();
break;
case 2:
fragment = null;
// Toast.makeText(getBaseContext(),"position 2",Toast.LENGTH_LONG).show();
break;
default:
break;
}
fragment = new TestFragment();
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// mDrawerList.setItemChecked(position, true);
// mDrawerList.setSelection(position);
// getActionBar().setTitle("Title changed");
// mDrawerLayout.closeDrawer(mDrawerList);
} else {
Toast.makeText(getBaseContext(),"Error in creating fragment",Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的片段类:
public class TestFragment extends Fragment implements View.OnClickListener {
private Button hideActionBarBtn;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.test_fragment,container,false);
hideActionBarBtn = (Button)view.findViewById(R.id.btn_hide_action_bar);
hideActionBarBtn.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v)
{
getActivity().getActionBar().hide();
Toast.makeText(getActivity().getBaseContext(),"Hide action bar",Toast.LENGTH_SHORT).show();
}
}
这是我学习android的测试代码。当我调用此方法隐藏操作栏时,它会抛出错误:
getActivity().getActionBar().hide().
为什么呢?这是正确的方法吗?我对android很新。
这是我在logcat中得到的:
01-18 00:49:33.475 12614-12614 / com.example.navigationdrawerbegin.navigationdrawerbegin W / dalvikvm:threadid = 1:线程退出时未捕获异常(组= 0xa6176908)01-18 00:49:33.479 12614- 12614 / com.example.navigationdrawerbegin.navigationdrawerbegin E / AndroidRuntime:FATAL EXCEPTION:main 01-18 00:49:33.479 12614-12614 / com.example.navigationdrawerbegin.navigationdrawerbegin E / AndroidRuntime:java.lang.NullPointerException 01-18 00: 49:33.479 12614-12614 / com.example.navigationdrawerbegin.navigationdrawerbegin E / AndroidRuntime:at com.example.navigationdrawerbegin.navigationdrawerbegin.TestFragment.onClick(TestFragment.java:32)01-18 00:49:33.479 12614-12614 / com .example.navigationdrawerbegin.navigationdrawerbegin E / AndroidRuntime:在android.view.View.performClick(View.java:4204)01-18 00:49:33.479 12614-12614 / com.example.navigationdrawerbegin.navigationdrawerbegin E / AndroidRuntime:at android .view.View $ PerformClick.run(View.java:17355)01-18 00:49:33.479 12614-12614 / com.example.navigationdrawerbegin.navigationdrawerbegin E / AndroidRuntime:at android.os.Handler.handleCallback(Handler.java:725)01-18 00:49:33.479 12614-12614 / com.example.navigationdrawerbegin.navigationdrawerbegin E / AndroidRuntime:at android.os.Handler.dispatchMessage(Handler.java:92)01-18 00:49:33.479 12614-12614 / com.example.navigationdrawerbegin.navigationdrawerbegin E / AndroidRuntime:at android.os.Looper.loop(Looper.java: 137)01-18 00:49:33.479 12614-12614 / com.example.navigationdrawerbegin.navigationdrawerbegin E / AndroidRuntime:at android.app.ActivityThread.main(ActivityThread.java:5041)01-18 00:49:33.479 12614- 12614 / com.example.navigationdrawerbegin.navigationdrawerbegin E / AndroidRuntime:at java.lang.reflect.Method.invokeNative(Native Method)01-18 00:49:33.479 12614-12614 / com.example.navigationdrawerbegin.navigationdrawerbegin E / AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)01-18 00:49:33.479 12614-12614 / com.example.navigationdr awerbegin.navigationdrawerbegin E / AndroidRuntime:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)01-18 00:49:33.479 12614-12614 / com.example.navigationdrawerbegin.navigationdrawerbegin E / AndroidRuntime :at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)01-18 00:49:33.479 12614-12614 / com.example.navigationdrawerbegin.navigationdrawerbegin E / AndroidRuntime:at dalvik.system.NativeStart。 main(原生方法)
答案 0 :(得分:0)
(代表问题作者发布)。
我找到了解决方案。这可行:
((ActionBarActivity)getActivity()).getSupportActionBar().hide();