0

我似乎无法找出这个错误的含义。大家能帮帮我吗?

这是我在 MainView.java 中的源代码。登录应用程序后,这是您看到的第一个屏幕,它应该显示标题,然后是 google maps 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);
4

1 回答 1

0

这意味着你NullPointerException在这条线上有一个: com.fotolife.app.MainView.onStart(MainView.java:37)

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

你需要添加这个:

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

并删除该onCreateView方法。

于 2013-01-06T19:38:29.493 回答