簡體   English   中英

.setOnClickListener上的空指針異常

[英]Null pointer Exception on .setOnClickListener

我遇到了一個登錄模式提交按鈕的點擊監聽器問題。

這是錯誤。

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等。

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

顯然,有更多與我認為的主題無關。 這是包含按鈕的對話框的XML文件。

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

任何幫助將不勝感激。

Submitnull因為它不是activity_main.xml一部分

當您在Activity調用findViewById ,它將在Activity的布局中查找View

試試這個:

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

另一件事:你用

android:layout_below="@+id/LoginTitle"

但你想要的可能是什么

android:layout_below="@id/LoginTitle"

有關@id@+id之間的區別,請參閱此問題

null對象引用上的android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)'

因為Submit按鈕在login_modal所以你需要使用loginDialog視圖來訪問按鈕:

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

嘗試在main.xml中為Button指定一個更具描述性的名稱,例如:

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

(在xml文件中使用小寫,至少是第一個字母)

然后在MainActivity類中,將其聲明為:

Button buttonXYZ;

在onCreate(Bundle savedInstanceState)方法中,將其定義為:

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

此外,將Buttons / TextViews移到外面並將它們放在.setOnClickListener之前 - 它使代碼更清晰。

Username = (EditText)findViewById(R.id.Username);
CompanyID = (EditText)findViewById(R.id.CompanyID);

當我放錯了代碼時,我也得到了類似的錯誤

text=(TextView)findViewById(R.id.text);// this line has to be below setcontentview
setContentView(R.layout.activity_my_otype);
//this is the correct place
text.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            }
    });

我讓它按照正確的順序放置代碼,如下所示

setContentView(R.layout.activity_my_otype);
text=(TextView)findViewById(R.id.text);
 text.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM