2015-01-28 21 views
16

Tôi gặp sự cố với trình nghe nhấp chuột cho nút gửi phương thức đăng nhập.Null pointer Ngoại lệ trên .setOnClickListener

Đây là lỗi.

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

Tôi có hiểu biết hợp lý về ngoại lệ con trỏ null và tôi đã tìm kiếm kỹ lưỡng vấn đề tương tự như của tôi. Tôi đã cố gắng để định dạng lại nghe nhấp chuột theo nhiều cách, chắc chắn tôi có quan điểm đúng ID, vv

package... 
import... 
public class MainActivity extends ActionBarActivity implements  NavigationDrawerFragment.NavigationDrawerCallbacks { 

    //Variables 
    String currentPage = ""; 
    Stack<String> crumbs = new Stack<String>(); 
    //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()}. 
    public CharSequence mTitle; 
    //temp 
    AuthenticateUserTokenResult authenticateUserTokenResult; 
    String loginErrorMessage = ""; 
    String loginErrorTitle = ""; 
    Boolean logonSuccessful = false; 
    Dialog loginDialog; 

    // Login EditTexts 
    EditText Username; 
    EditText CompanyID; 
    EditText Password; 
    Button Submit; 

    @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)); 

     if(authenticateUserTokenResult == null) { 
      attemptLogin(); 
     } 
    } 

    public void attemptLogin() { 
     loginDialog = new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar); 
     loginDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     loginDialog.setContentView(R.layout.login_modal); 
     loginDialog.setCancelable(false); 
     //loginDialog.setOnCancelListener(cancelListener); 
     loginDialog.show(); 
     Submit = (Button)findViewById(R.id.Submit); 
     Submit.setOnClickListener(new View.OnClickListener() // the error is on this line (specifically the .setOnClickListener) 
     { 
      @Override 
      public void onClick(View v) 
      { 
       ClyxUserLogin user = new ClyxUserLogin(); 
       Username = (EditText)findViewById(R.id.Username); 
       user.logon = Username.getText().toString(); 
       CompanyID = (EditText)findViewById(R.id.CompanyID); 
       user.idCompany = Integer.parseInt(CompanyID.getText().toString()); 
       Password = (EditText)findViewById(R.id.Password); 
       user.password = Password.getText().toString(); 
       user.idApplication = 142; 
       authenticate(user); 
      } 
     }); 
    } 

Có nhiều, rõ ràng, nhưng không liên quan đến chủ đề tôi nghĩ. Đây là tệp XML cho hộp thoại có nút trên đó.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#3366FF"> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:background="#FFFFFF" > 

     <TextView 
      android:id="@+id/LoginTitle" 
      android:layout_width="200dp" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" 
      android:layout_marginTop="10dp" 
      android:layout_marginStart="10dp" 
      android:textColor="#000000" 
      android:textSize="20sp" 
      android:text="Login" /> 

     <EditText 
      android:id="@+id/Username" 
      android:layout_width="200dp" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/LoginTitle" 
      android:layout_margin="10dp" 
      android:hint="Username" /> 

     <EditText 
      android:id="@+id/CompanyID" 
      android:layout_width="200dp" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/Username" 
      android:layout_alignStart="@+id/Username" 
      android:inputType="number" 
      android:hint="Company ID" /> 

     <EditText 
      android:id="@+id/Password" 
      android:layout_width="200dp" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/CompanyID" 
      android:layout_alignStart="@+id/Username" 
      android:layout_marginTop="10dp" 
      android:layout_marginBottom="10dp" 
      android:inputType="textPassword" 
      android:hint="Password" /> 

     <Button 
      android:id="@+id/Submit" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/Password" 
      android:layout_marginBottom="10dp" 
      android:layout_centerHorizontal="true" 
      android:text="Login" /> 

    </RelativeLayout> 

</RelativeLayout> 

Mọi trợ giúp sẽ được đánh giá cao.

+9

Quay lại đây gần một năm sau và cảm thấy tốt khi tôi cải thiện và thoát khỏi những sai lầm ngớ ngẩn như thế này. Mọi người đều bắt đầu ở đâu đó và tôi yêu rằng Stack sẽ hỗ trợ bạn từng bước. –

Trả lời

47

Submitnull vì nó không phải là một phần của activity_main.xml

Khi bạn gọi findViewById bên trong một Activity, nó sẽ tìm một View bên trong bố cục Hoạt động của bạn.

thử này để thay thế:

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

Một điều: bạn sử dụng

android:layout_below="@+id/LoginTitle" 

nhưng những gì bạn muốn có lẽ là

android:layout_below="@id/LoginTitle" 

Xem this question về sự khác biệt giữa @id@+id.

+2

Cảm ơn bạn, một thói quen tốt để có được vào thay vì sao chép dán id khác. Tôi đã không hiểu ý nghĩa của + trước đó. Không bao giờ nghĩ để nhìn vào nó. (Thêm vào như được chấp nhận cho một câu trả lời hơi phát triển hơn và một chút trợ giúp thêm) –

8

android.widget.Button.setOnClickListener (android.view.View $ OnClickListener)' về một tham chiếu đối tượng null

Submit nút nằm bên login_modal vì vậy bạn cần phải sử dụng loginDialog nhằm truy cập nút:

Submit = (Button)loginDialog.findViewById(R.id.Submit); 
+2

Wow làm thế nào tôi không thấy điều đó. Tôi nghĩ rằng nó bởi vì ngoại lệ chỉ vào dòng dưới đây. Cảm ơn sự giúp đỡ –

0

Hãy thử đưa ra nút của bạn trong main.xml Đặt tên mô tả nhiều hơn như:

<Button 
       android:id="@+id/buttonXYZ" 

(sử dụng chữ thường trong các tập tin xml của bạn, ít nhất, chữ cái đầu tiên)

Và sau đó trong bạn lớp MainActivity, tuyên bố nó như là:

Button buttonXYZ;

Trong phương pháp onCreate (Bundle savedInstanceState) của bạn, xác định nó như:

buttonXYZ = (Button) findViewById(R.id.buttonXYZ); 

Ngoài ra, di chuyển các nút/TextViews ra ngoài và đặt chúng trước .setOnClickListener - nó làm cho mã sạch hơn.

Username = (EditText)findViewById(R.id.Username); 
CompanyID = (EditText)findViewById(R.id.CompanyID); 
Các vấn đề liên quan