java我刚刚开始安卓开发,当我制作这个应用程序时,它在调试时不起作用
这个应用程序是关于点击一个Button
的,有一个TextBox
在点击时会改变它的内容
主要活动。java
package com.lost.eventhandling;
import 安卓.os.Bundle;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.support.v7.widget.Toolbar;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button willybutton=(Button)findViewById(R.id.willybutton);
assert willybutton != null;
willybutton.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v){
TextView willytext=(TextView)findViewById(R.id.willytext);
willytext.setText("I DID IT ");
}
}
);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
现在来看看我的xml代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.lost.eventhandling.MainActivity"
安卓:background="#71f8bd">
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceLarge"
安卓:text="@string/willy_text"
安卓:id="@+id/willytext"
安卓:textColor="#e83858"
安卓:layout_alignParentTop="true"
安卓:layout_centerHorizontal="true"
安卓:layout_marginTop="179dp" />
<Button
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/willy_button"
安卓:id="@+id/willybutton"
安卓:layout_centerVertical="true"
安卓:layout_centerHorizontal="true"
安卓:background="#764045"/>
</RelativeLayout>
每当我调试我的应用程序时,它都会发送一条消息,它会意外地停止。 我的安卓显示器给了我这个红色的文本:
FATAL EXCEPTION: main
Process: com.lost.eventhandling, PID: 2449
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lost.eventhandling/com.lost.eventhandling.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void 安卓.widget.Button.setOnClickListener(安卓.view.View$OnClickListener)' on a null object reference
at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at 安卓.app.ActivityThread.access$800(ActivityThread.java:151)
at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at 安卓.os.Handler.dispatchMessage(Handler.java:102)
at 安卓.os.Looper.loop(Looper.java:135)
at 安卓.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void 安卓.widget.Button.setOnClickListener(安卓.view.View$OnClickListener)' on a null object reference
at com.lost.eventhandling.MainActivity.onCreate(MainActivity.java:26)
at 安卓.app.Activity.performCreate(Activity.java:5990)
at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at 安卓.app.ActivityThread.access$800(ActivityThread.java:151)
at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at 安卓.os.Handler.dispatchMessage(Handler.java:102)
at 安卓.os.Looper.loop(Looper.java:135)
at 安卓.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:698)
# 1 楼答案
用以下代码行替换您的
OnclickListener
代码Button
: