尝试在空对象引用上调用虚拟方法“void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)”
Posted
技术标签:
【中文标题】尝试在空对象引用上调用虚拟方法“void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)”【英文标题】:Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 【发布时间】:2015-08-11 01:28:44 【问题描述】:问题如下。我有一个登录活动(在 android Studio 中),几天前运行良好。我不记得有任何更改,但是当我上次运行这个时,应用程序在我单击登录按钮后立即关闭。最后指出的是关于预执行 AsyncTask 的祝酒词。 而且我不明白为什么会出现 NullPointerException。 我的注册活动代码几乎相同,而且运行良好。
这是日志:
05-28 16:04:52.395 1218-1232/system_process V/WindowManager﹕ addAppToken: AppWindowToken5d89eb token=Token23ccc93a ActivityRecord2fe54865 u0 utanashati.reminder/.HomepageActivity t17 to stack=1 task=17 at 1
05-28 16:04:52.407 19927-19927/utanashati.reminder D/AndroidRuntime﹕ Shutting down VM
05-28 16:04:52.408 19927-19927/utanashati.reminder E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: utanashati.reminder, PID: 19927
java.lang.RuntimeException: Unable to start activity
ComponentInfoutanashati.reminder/utanashati.reminder.HomepageActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at utanashati.reminder.HomepageActivity.onCreate(HomepageActivity.java:55)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
05-28 16:04:52.410 1218-1232/system_process W/ActivityManager﹕ Force finishing activity 1 utanashati.reminder/.HomepageActivity
05-28 16:04:52.411 1218-1232/system_process W/ActivityManager﹕ Force finishing activity 2 utanashati.reminder/.LoginActivity
编辑 1
我睁大了眼睛,问题不在于LoginActivity,而在于HomepageActivity。代码如下:
import ...
public class HomepageActivity extends Activity implements AdapterView.OnItemSelectedListener
protected EditText mAddTaskText;
protected Spinner mPrioritySpinner;
protected Button mAddTaskButton;
protected int intPriority = 0;
protected String taskText;
protected Timestamp taskTimestamp;
protected Task userTask;
protected JsonGenerator taskJSON;
@Override
protected void onCreate(Bundle savedInstanceState) // Starts activity. The state can be restored from savedInstanceState
super.onCreate(savedInstanceState); // Calls the superclass method (IMPORTANT)
setContentView(R.layout.activity_homepage); // Sets layout from activity_homepage.xml
mPrioritySpinner = (Spinner) findViewById(R.id.prioritySpinner); // Creates an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.priorityList, android.R.layout.simple_spinner_item); // Specifies the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Applies the adapter to the spinner
mPrioritySpinner.setAdapter(adapter);
mPrioritySpinner.setOnItemSelectedListener(this);
mAddTaskText = (EditText) findViewById(R.id.addTaskEditText); // Finds View by its id in .xml file
mAddTaskButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Toast.makeText(HomepageActivity.this, "Done!", Toast.LENGTH_LONG).show();
Calendar taskCalendar = Calendar.getInstance(); // Creates new calendar
long taskTime = taskCalendar.getTimeInMillis(); // Gets time in milliseconds
taskTimestamp = new Timestamp(taskTime); // Creates new Timestamp
taskText = mAddTaskText.getText().toString(); // Gets description of the task
userTask.setDate(taskTimestamp); // Sets date
userTask.setText(taskText); // Sets text
/* Creating JsonGenerator */
ObjectMapper mapper = new ObjectMapper();
try
mapper.writeValue(taskJSON, userTask);
catch (IOException e)
Toast.makeText(HomepageActivity.this, "Could not create JSON", Toast.LENGTH_LONG).show();
/* Getting out email and password */
String userPassword = ((EmailPassword) HomepageActivity.this.getApplication()).getPassword();
String userEmail = ((EmailPassword) HomepageActivity.this.getApplication()).getUserEmail();
Toast.makeText(HomepageActivity.this, userEmail + " " + userPassword, Toast.LENGTH_LONG).show();
/* HTTP stuff */
HttpPoster get = new HttpPoster();
get.execute(userEmail, userPassword, taskJSON.toString());
);
public int getData (String username, String password, String taskJSON)
try
HttpPost httpPost = new HttpPost("http://something.com/" + username + "/tasks");
String dataToEncode = username + ":" + password;
String encodedData = Base64.encodeToString(dataToEncode.getBytes(), Base64.NO_WRAP);
httpPost.setHeader("Authorization", encodedData);
try
StringEntity taskEntity = new StringEntity(taskJSON, "UTF-8");
httpPost.setEntity(taskEntity);
catch (UnsupportedEncodingException e)
Toast.makeText(HomepageActivity.this, "Unsupported encoding", Toast.LENGTH_LONG).show();
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200)
return 1;
else if (statusCode == 404) return 2;
else if (statusCode == 500) return 3;
else if (statusCode == 409) return 4;
else return statusCode;
catch (IOException e)
e.printStackTrace();
return 0;
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id)
String priority = parent.getItemAtPosition(pos).toString(); // Gets chosen priority
Toast.makeText(HomepageActivity.this, priority, Toast.LENGTH_LONG).show();
while (!((priority.equals("Low")) || (priority.equals("Medium")) || (priority.equals("High"))))
Toast.makeText(HomepageActivity.this, "Something bad happened. Try to choose again", Toast.LENGTH_LONG).show();
if (priority.equals("Low"))
intPriority = 0;
else if (priority.equals("Medium"))
intPriority = 1;
else if (priority.equals("High"))
intPriority = 2;
userTask.setPriority(intPriority); // Sets chosen priority
public void onNothingSelected(AdapterView<?> parent)
userTask.setPriority(intPriority); // Sets default priority ("0")
public class HttpPoster extends AsyncTask<String, Void, Integer>
@Override
protected void onPreExecute()
super.onPreExecute();
@Override
protected Integer doInBackground(String... params)
return getData(params[0], params[1], params[3]);
@Override
protected void onPostExecute(Integer result)
super.onPostExecute(result);
if (result == 1)
Toast.makeText(HomepageActivity.this, "Login successful", Toast.LENGTH_LONG).show();
Intent takeUserHome = new Intent(HomepageActivity.this, HomepageActivity.class);
startActivity(takeUserHome);
else if (result == 2)
Toast.makeText(HomepageActivity.this, "No such user", Toast.LENGTH_LONG).show();
else if (result == 3)
Toast.makeText(HomepageActivity.this, "Internal server error: unable to send email", Toast.LENGTH_LONG).show();
else if (result == 4)
Toast.makeText(HomepageActivity.this, "Task already exists", Toast.LENGTH_LONG).show();
else
Toast.makeText(HomepageActivity.this, result.toString(), Toast.LENGTH_LONG).show();
还有XML文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="utanashati.testapp.HomepageActivity">
<EditText
android:layout_
android:layout_
android:hint="Add a new task..."
android:id="@+id/addTaskEditText"
android:nestedScrollingEnabled="false"
android:minLines="1"
android:maxLines="1" />
<Spinner
android:layout_
android:layout_
android:id="@+id/prioritySpinner"
android:layout_alignRight="@+id/addTaskButton"
android:layout_alignEnd="@+id/addTaskButton"
android:layout_below="@+id/addTaskEditText" />
<Button
android:layout_
android:layout_
android:text="Add task"
android:id="@+id/addTaskButton"
android:layout_below="@+id/prioritySpinner"
android:layout_centerHorizontal="true" />
</RelativeLayout>
【问题讨论】:
您发布的堆栈跟踪指向HomepageActivity
中的问题,而不是您发布代码的LoginActivity
。
你在 HomepageActivity 活动中的错误,你能把这段代码放在这里吗?
@ci_ 哇,你让我大开眼界,谢谢 :)
你能指定解决这个问题的方法吗
【参考方案1】:
mAddTaskButton.setOnClickListener(new View.OnClickListener()
您有一个点击列表器,但您尚未使用布局绑定初始化 mAddTaskButton
【讨论】:
【参考方案2】:检查您在 Java 和 XML 中是否有匹配的 ID
【讨论】:
欢迎来到 SO。这可能应该是评论,而不是答案...【参考方案3】:遇到同样的错误, 检查这个:小错误
检查 findviewbyid(R.id.yourID); 如果您输入的 id 是否正确。
【讨论】:
【参考方案4】:将setOnClickListener
放入onStart
方法为我解决了这个问题。
查看“Android 生命周期概念”以获得进一步说明
【讨论】:
【参考方案5】:我遇到了同样的问题,似乎我没有启动与点击侦听器一起使用的按钮,换句话说,id 没有启动
【讨论】:
“没有”?领带,你是说?【参考方案6】:只需在类的顶部将按钮定义为 lateinit var:
lateinit var buttonOk: Button
当您想在另一个布局中使用按钮时,您应该在该布局中定义它。例如,如果您想在布局中使用名称为“dialogview”的按钮,则应编写:
buttonOk = dialogView.findViewById<Button>(R.id.buttonOk)
在此之后,您可以对按钮使用 setonclicklistener 并且不会出现任何错误。 你可以看到这个问题的正确答案:Android Kotlin findViewById must not be null
【讨论】:
【参考方案7】:查看此解决方案。它对我有用...... 检查引发错误的按钮的 id...它可能与您应用程序的其他页面中的任何一个相同。 如果是,则更改它们的id,然后应用程序就可以完美运行了。
我在两个不同的 XML 代码中有两个相同的按钮 ID....我更改了 ID。现在它完美运行!! 希望有效
【讨论】:
【参考方案8】:您正在调用的按钮似乎不在您在setContentView(R.layout.your_layout)
中使用的布局中
检查一下。
【讨论】:
【参考方案9】:确保在使用时: 按钮 "varName" =findViewById("btID"); 你输入了正确的“btID”。我不小心从另一个类似的活动中输入了一个按钮的 id,它显示了同样的错误。希望对您有所帮助。
【讨论】:
【参考方案10】:没错,Mustafa....它的工作原理..它指向两个布局
-
setContentView(R.layout.your_layout)
v23(your_layout)。
您应该同时采用 Button 活动布局... 成功解决这个问题
【讨论】:
【参考方案11】:mAddTaskButton
为空,因为您从未使用以下方法对其进行初始化:
mAddTaskButton = (Button) findViewById(R.id.addTaskButton);
在您致电mAddTaskButton.setOnClickListener()
之前。
【讨论】:
我也有同样的问题,但是我已经正确初始化了。 投射到按钮是多余的以上是关于尝试在空对象引用上调用虚拟方法“void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)”的主要内容,如果未能解决你的问题,请参考以下文章
尝试在空对象引用上调用虚拟方法 com.google.firebase.iid.FirebaseInstanceId.getInstanceId()'
NullPointerException 错误尝试在空对象引用上调用虚拟方法错误
AsyncTask +数据库尝试在空对象引用上调用虚拟方法[重复]
数据库错误:尝试在空对象引用上调用虚拟方法“Cursor .getScene()”[重复]
尝试在空对象引用上调用虚拟方法 'android.view.SurfaceHolder android.view.SurfaceView.getHolder()'