0

Navigation Выдвижные активностьпанель навигации активность Null NullPointerException

У меня возникли проблемы определения кнопки OnClick функции в OnCreate, NullPointerException и закрыть приложение

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" /> 

Функция открытия (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"); 
     } 
    }); 

} 

, но если я определить OnClick в кнопке XML хорошо работает

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" /> 

+0

Можете выложить ошибку logcat? – Neige

+0

, где вы вызываете 'changeText'? –

+0

android: onClick = "changeText" вызов функции changeText – LionThinKing

ответ

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

Надеюсь, это поможет.

+0

button = (кнопка) mNavigationDrawerFragment .findViewById (R.id.button); не имеет findViewById, но я тестирую 10 Кнопка = (кнопка) mNavigationDrawerFragment.getView(). FindViewById (R.id.button); но ошибка все еще – LionThinKing

+0

Извините, что я забыл о getView() – Nullpoint

Смежные вопросы