.setOnClickListener上的空指针异常 [英] Null pointer Exception on .setOnClickListener

查看:233
本文介绍了.setOnClickListener上的空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到登录模式提交按钮的点击监听器问题。

I am having an issue with a click listener for a login modal submit button.

这是错误。

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

I have a reasonable understanding of what a null pointer exception is and I have search thoroughly for an issue similar to mine. I have tried to reformat the click listener in several ways, made sure I have the correct view ID etc.

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文件。

There is more, obviously, but not relevant to the topic I think. Here is the XML file for the dialog that has the button on it.

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

任何帮助将不胜感激。

Any help would be greatly appreciated.

推荐答案

提交 null 因为它不是 activity_main.xml的一部分

当你打电话给 findViewById 活动中,它将在Activity的布局中寻找 View

When you call findViewById inside an Activity, it is going to look for a View inside your Activity's layout.

试试这个:

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

另一件事:你使用

android:layout_below="@+id/LoginTitle"

但是你想要的可能是

android:layout_below="@id/LoginTitle"

请参阅这个问题关于 @id @ + id 之间的区别。

See this question about the difference between @id and @+id.

这篇关于.setOnClickListener上的空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆