无法加载Fragment Activity

时间:2013-01-06 19:37:06

标签: android android-fragments

我似乎无法找出此错误的含义。你能帮助我吗?

这是我在MainView.java中的源代码。登录应用程序后,这是您看到的第一个屏幕,它应该显示标题,然后是谷歌地图api,然后是底部的菜单。

public class MainView extends FragmentActivity implements OnClickListener {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.main_view, container, false);
    }


    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();

        Fragment fragment;

        Button openDrawer = (Button) findViewById(R.id.openDrawer);
        openDrawer.setOnClickListener(this);

        Button openUploader = (Button) findViewById(R.id.menuButtonAdd);
        openUploader.setOnClickListener(this);
        Button openEditor = (Button) findViewById(R.id.menuButtonEdit);
        openEditor.setOnClickListener(this);
        Button openMap = (Button) findViewById(R.id.menuButtonMap);
        openMap.setOnClickListener(this);
        Button openPeople = (Button) findViewById(R.id.menuButtonPeople);
        openPeople.setOnClickListener(this);

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        Map myFragment = new Map();
        ft.add(R.id.mainFragment, myFragment);
        ft.commit();
    }


    public void onClick(View arg0) {
        Fragment newFragment = new Map();

        switch (arg0.getId()) {
        case(R.id.openDrawer):
            SlidingDrawer menuDrawer = (SlidingDrawer) findViewById(R.id.menuDrawer);
            //SlidingDrawer textDrawer = (SlidingDrawer) findViewById(R.id.textDrawer);
            if (menuDrawer.isOpened() == true) {
                menuDrawer.animateClose();
                //textDrawer.animateClose();
            } else if (menuDrawer.isOpened() == false) {
                menuDrawer.animateOpen();
                //textDrawer.animateOpen();
            }
            break;
        case(R.id.menuButtonAdd):
            newFragment = new UploadFragment();
            break;
        case(R.id.menuButtonEdit):
            newFragment = new EditGallery();
            break;
        case(R.id.menuButtonMap):
            newFragment = new Map();
            break;
        case(R.id.menuButtonPeople):
            // TODO present an error
            break;
        default:
            newFragment = new Map();
            break;
        }

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.mainFragment, newFragment);
        transaction.commit();
    }
}

第37行的代码:

openDrawer.setOnClickListener(this);

1 个答案:

答案 0 :(得分:0)

这意味着您在此行中有NullPointerExceptioncom.fotolife.app.MainView.onStart(MainView.java:37)

这是因为openDrawernull,因为在视图中找不到它。

您需要添加以下内容:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_view);
}

并删除onCreateView方法。