导航抽屉活动Null NullPointerException

时间:2015-02-04 14:30:45

标签: android nullpointerexception navigation-drawer buttonclick

导航抽屉活动

我在OnCreate,NullPointerException和关闭app中定义按钮onclick函数时遇到问题

fragment_main.xml     

<TextView android:id="@+id/section_label" android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_below="@+id/section_label"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="206dp" />

Oncreate function(MainActivity.java)

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

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));


    final Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v)
        {
            button.setText("Button Text Change");
        }
    });

}

但如果我在xml按钮中定义onclick效果很好

public void changeText(View v)
{
    final Button button = (Button) findViewById(R.id.button);
    button.setText("Button Text Change");
}

fragment_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment">

<TextView android:id="@+id/section_label" android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_below="@+id/section_label"
    android:layout_centerHorizontal="true"
    android:onClick="changeText"
    android:layout_marginTop="206dp" />

1 个答案:

答案 0 :(得分:1)

如果您希望获得 参考 按钮,则需要使用findViewById中的fragment,例如

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

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));


    final Button button = (Button) mNavigationDrawerFragment.getView () .findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v)
        {
            button.setText("Button Text Change");
        }
    });

}

你在这里做错了:

public void changeText(View v)
{
    final Button button = (Button) mNavigationDrawerFragment .findViewById(R.id.button);
    button.setText("Button Text Change");
}

更有效的方法是在class级别设置成员变量,例如mNavigationDrawerFragment,例如:

Button button;

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

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            //Al setup stuff you have....


    button = (Button) mNavigationDrawerFragment.getView () .findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v)
        {
            button.setText("Button Text Change");
        }
    });

}
public void changeText(String s)
{
    button.setText(s);
}

Button Android Documentation

一样,有很多有用的信息

希望它有所帮助。