Java .setOnClickListener上的空指针异常
我对登录模式提交按钮的单击侦听器有问题 这就是错误所在Java .setOnClickListener上的空指针异常,java,android,nullpointerexception,onclicklistener,Java,Android,Nullpointerexception,Onclicklistener,我对登录模式提交按钮的单击侦听器有问题 这就是错误所在 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 我对空指针异常有一个合理的理解,并且我已经彻底地搜索了一个类似于我的问
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);
}
});
}
包。。。
导入。。。
公共类MainActivity扩展ActionBarActivity实现NavigationDrawerFragment.NavigationDrawerCallbacks{
//变数
字符串currentPage=“”;
堆栈碎屑=新堆栈();
//片段管理导航抽屉的行为、交互和表示。
私人导航抽屉碎片mnavigation抽屉碎片;
//用于存储最后一个屏幕标题。用于{@link#restoreActionBar()}。
公共字符序列mTitle;
//临时工
AuthenticateUserTokenResult AuthenticateUserTokenResult;
字符串loginErrorMessage=“”;
字符串loginErrorTitle=“”;
布尔logonSuccessful=false;
对话框登录对话框;
//登录编辑文本
编辑文本用户名;
编辑文本公司ID;
编辑文本密码;
按钮提交;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment=(NavigationDrawerFragment)getSupportFragmentManager().findFragmentById(R.id.navigation\u抽屉);
mTitle=getTitle();//设置抽屉。
mNavigationDrawerFragment.setUp(R.id.navigation_drawer,(DrawerLayout)findViewById(R.id.drawer_布局));
if(authenticateUserTokenResult==null){
尝试登录();
}
}
public void attemptLogin(){
loginDialog=新建对话框(这是android.R.style.Theme\u transparent\u NoTitleBar);
loginDialog.requestWindowFeature(Window.FEATURE\u NO\u TITLE);
loginDialog.setContentView(R.layout.login_模式);
loginDialog.setCancelable(假);
//loginDialog.setOnCancelListener(取消侦听器);
loginDialog.show();
提交=(按钮)findViewById(R.id.Submit);
Submit.setOnClickListener(new View.OnClickListener()//错误在这一行(特别是.setOnClickListener)
{
@凌驾
公共void onClick(视图v)
{
ClyxUserLogin user=新的ClyxUserLogin();
用户名=(EditText)findViewById(R.id.Username);
user.logon=Username.getText().toString();
CompanyID=(EditText)findViewById(R.id.CompanyID);
user.idCompany=Integer.parseInt(CompanyID.getText().toString());
密码=(EditText)findViewById(R.id.Password);
user.password=password.getText().toString();
user.idApplication=142;
认证(用户);
}
});
}
显然,还有更多,但与我认为的主题无关。
下面是对话框的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>
任何帮助都将不胜感激
android.widget.Button.setOnClickListener(android.view.view$OnClickListener)'
关于空对象引用
由于Submit
按钮位于login\u modal
内,因此您需要使用loginDialog
查看以访问按钮:
Submit = (Button)loginDialog.findViewById(R.id.Submit);
Submit
为null
,因为它不是activity\u main.xml
在活动
中调用findViewById
时,它将在活动的布局中查找视图
请尝试以下方法:
Submit = (Button)loginDialog.findViewById(R.id.Submit);
另一件事:你用
android:layout_below="@+id/LoginTitle"
但你想要的可能是
android:layout_below="@id/LoginTitle"
请参阅
@id
和@+id
之间的区别,尝试为main.xml中的按钮提供更具描述性的名称,例如:
<Button
android:id="@+id/buttonXYZ"
另外,将按钮/文本视图移到外面,并将它们放在.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) {
}
});
哇,我怎么没看到。我想这是因为异常指向下面的那一行。谢谢你的帮助。如果我的按钮不在
login_modal
内怎么办,但我仍然收到相同的错误,@ρ∑ρρK??谢谢你,这是一个好习惯,可以养成,而不是复制粘贴其他id。我以前不理解+的意义。Never想去调查一下。(添加了一个更成熟的答案和一点额外的帮助)我有一个类似的问题,但是我的按钮不在对话框窗口中。我如何纠正它,@2Dee?@DerryckDX如果没有看到您的代码,很难知道,我们不应该在评论部分这样做。请在检查重复项后发布新问题。别忘了包括所有相关代码和错误堆栈跟踪。回到这里,阿尔莫斯t一年后,我感觉很好,我已经进步了很多,从像这样愚蠢的错误中学习。每个人都从某个地方开始,我喜欢堆栈将支持你的每一步。我仍然有这个问题,只是我的按钮不在登录对话框中。
。我如何修复它,@Jack.Ramsden?
setContentView(R.layout.activity_my_otype);
text=(TextView)findViewById(R.id.text);
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});