FindViewByID在setOnClickListener上返回null

时间:2015-04-02 12:00:00

标签: android android-fragments onclicklistener

当我尝试开始我的活动时,我收到以下错误。

E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.proofoftraining, PID: 19856
java.lang.NullPointerException at com.example.app.MainActivity.setupAddActivityButton

这是MainActivity代码的一部分:

...
import android.app.Activity;
import android.content.Context;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.content.Intent;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;

...

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {

/**
 * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
 */
private NavigationDrawerFragment mNavigationDrawerFragment;

/**
 * Used to store the last screen title. For use in {@link #restoreActionBar()}.
 */
...

private ActivitiesListAdapter adapter;

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();

//Set up the Activity List
setupActivityListViewAdapter();
setupAddActivityButton();

return true;
}
return super.onCreateOptionsMenu(menu);
}




private void setupActivityListViewAdapter() {
adapter = new ActivitiesListAdapter(MainActivity.this, R.layout.item_activities, new ArrayList<activity>());
ListView ActivitiesListView = (ListView)findViewById(R.id.listView_activities);
ActivitiesListView.setAdapter(adapter);
}
private void setupAddActivityButton() {
findViewById(R.id.action_activity_add).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.insert(new activity(0,0), 0); //Test activity(0,0)
}
});
}
}

我的菜单main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
    <item android:id="@+id/action_activity_add"
        android:icon="@drawable/ic_action_new"
        android:title="@string/action_activity_add"
        app:showAsAction="ifRoom|withText" />
</menu>

我应该在哪里实现setupAddActivityButton(), 这样findViewByID不会为空?

感谢。

2 个答案:

答案 0 :(得分:0)

您可以使用以下代码处理菜单点击事件

    @Override
    public boolean onOptionsItemSelected(MenuItem p_item)
    {
        switch (p_item.getItemId())
        {
            case R.id.action_activity_add:
                // Write your code here.
                break;

            default:
                break;
        }
        return super.onOptionsItemSelected(p_item);
    }

答案 1 :(得分:0)

findViewVyId()无法找到

菜单项。

您应该查看如何在Android活动中使用menuonPrepareOptionsMenu()onOptionsItemSelected()

e.g。你可以查看这个教程,它非常简单&amp;直截了当 android menu tutorial