java – 在.setOnClickListener上的空指针异常

前端之家收集整理的这篇文章主要介绍了java – 在.setOnClickListener上的空指针异常前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我遇到一个点击监听器的登录模式提交按钮的问题.

这是错误.

  1. Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

我有一个合理的理解,一个空指针异常是什么,我有一个类似于我的问题彻底搜索.我已经尝试以多种方式重新格式化点击监听器,确保我有正确的视图ID等.

  1. package...
  2. import...
  3. public class MainActivity extends ActionBarActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks {
  4.  
  5. //Variables
  6. String currentPage = "";
  7. Stack<String> crumbs = new Stack<String>();
  8. //Fragment managing the behaviors,interactions and presentation of the navigation drawer.
  9. private NavigationDrawerFragment mNavigationDrawerFragment;
  10. // Used to store the last screen title. For use in {@link #restoreActionBar()}.
  11. public CharSequence mTitle;
  12. //temp
  13. AuthenticateUserTokenResult authenticateUserTokenResult;
  14. String loginErrorMessage = "";
  15. String loginErrorTitle = "";
  16. Boolean logonSuccessful = false;
  17. Dialog loginDialog;
  18.  
  19. // Login EditTexts
  20. EditText Username;
  21. EditText CompanyID;
  22. EditText Password;
  23. Button Submit;
  24.  
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_main);
  29.  
  30. mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
  31. mTitle = getTitle(); // Set up the drawer.
  32. mNavigationDrawerFragment.setUp(R.id.navigation_drawer,(DrawerLayout) findViewById(R.id.drawer_layout));
  33.  
  34. if(authenticateUserTokenResult == null) {
  35. attemptLogin();
  36. }
  37. }
  38.  
  39. public void attemptLogin() {
  40. loginDialog = new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar);
  41. loginDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  42. loginDialog.setContentView(R.layout.login_modal);
  43. loginDialog.setCancelable(false);
  44. //loginDialog.setOnCancelListener(cancelListener);
  45. loginDialog.show();
  46. Submit = (Button)findViewById(R.id.Submit);
  47. Submit.setOnClickListener(new View.OnClickListener() // the error is on this line (specifically the .setOnClickListener)
  48. {
  49. @Override
  50. public void onClick(View v)
  51. {
  52. ClyxUserLogin user = new ClyxUserLogin();
  53. Username = (EditText)findViewById(R.id.Username);
  54. user.logon = Username.getText().toString();
  55. CompanyID = (EditText)findViewById(R.id.CompanyID);
  56. user.idCompany = Integer.parseInt(CompanyID.getText().toString());
  57. Password = (EditText)findViewById(R.id.Password);
  58. user.password = Password.getText().toString();
  59. user.idApplication = 142;
  60. authenticate(user);
  61. }
  62. });
  63. }

显而易见,但与我认为的话题无关.
以下是对话框的XML文件,其上有按钮.

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="#3366FF">
  6.  
  7. <RelativeLayout
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_centerInParent="true"
  11. android:background="#FFFFFF" >
  12.  
  13. <TextView
  14. android:id="@+id/LoginTitle"
  15. android:layout_width="200dp"
  16. android:layout_height="wrap_content"
  17. android:gravity="center_horizontal"
  18. android:layout_marginTop="10dp"
  19. android:layout_marginStart="10dp"
  20. android:textColor="#000000"
  21. android:textSize="20sp"
  22. android:text="Login" />
  23.  
  24. <EditText
  25. android:id="@+id/Username"
  26. android:layout_width="200dp"
  27. android:layout_height="wrap_content"
  28. android:layout_below="@+id/LoginTitle"
  29. android:layout_margin="10dp"
  30. android:hint="Username" />
  31.  
  32. <EditText
  33. android:id="@+id/CompanyID"
  34. android:layout_width="200dp"
  35. android:layout_height="wrap_content"
  36. android:layout_below="@+id/Username"
  37. android:layout_alignStart="@+id/Username"
  38. android:inputType="number"
  39. android:hint="Company ID" />
  40.  
  41. <EditText
  42. android:id="@+id/Password"
  43. android:layout_width="200dp"
  44. android:layout_height="wrap_content"
  45. android:layout_below="@+id/CompanyID"
  46. android:layout_alignStart="@+id/Username"
  47. android:layout_marginTop="10dp"
  48. android:layout_marginBottom="10dp"
  49. android:inputType="textPassword"
  50. android:hint="Password" />
  51.  
  52. <Button
  53. android:id="@+id/Submit"
  54. android:layout_width="wrap_content"
  55. android:layout_height="wrap_content"
  56. android:layout_below="@+id/Password"
  57. android:layout_marginBottom="10dp"
  58. android:layout_centerHorizontal="true"
  59. android:text="Login" />
  60.  
  61. </RelativeLayout>
  62.  
  63. </RelativeLayout>

任何帮助将不胜感激.

解决方法

Submit是null,因为它不是activity_main.xml的一部分

当您在Activity中调用findViewById时,将在Activity的布局中查找一个View.

改为:

  1. Submit = (Button)loginDialog.findViewById(R.id.Submit);

另一件事:你使用

  1. android:layout_below="@+id/LoginTitle"

但你想要的是可能的

  1. android:layout_below="@id/LoginTitle"

请参阅this question关于@id和@ id之间的区别.

猜你在找的Java相关文章