我有2个按钮,第一个工作正常但不是第二个按钮(button5)。它在点击时崩溃了应用程序。
遇到的错误是:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
我在XML文件中定义了按钮:
<Button
android:id="@+id/button"
android:layout_width="182dp"
android:layout_height="49dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="135dp"
android:background="@drawable/pay"
android:fontFamily="@font/roboto"
android:text="Login"/>
<Button
android:id="@+id/button5"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="-136dp"
android:fontFamily="@font/roboto"
android:text="@string/create_new_account"
android:textSize="12sp" />
并称之为:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
Button button5 = (Button) findViewById(R.id.button5);
}
public void onClick(View v) {
if (v.getId() == R.id.button) {
Intent intent = new Intent(MainActivity.this, detailspage.class);
startActivity(intent);
} else if (v.getId() == R.id.button5) {
Intent intent = new Intent(MainActivity.this, Registration.class);
startActivity(intent);
}
}
我的清单文件是:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".detailspage"/>
<activity android:name=".Registration"/>
<activity android:name=".confirmBicycle"></activity>
</application>
我已经经历了许多类似的问题并尝试了不同的编写代码的方法,但无济于事。谢谢你的建议!
答案 0 :(得分:1)
button.setOnClickListener(this);
button5.setOnClickListener(this);
您正在活动中实现接口,但未将活动实例传递给侦听器。
答案 1 :(得分:0)
我建议你试试:
Button button5 = (Button) findViewById(R.id.button5);
button5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// your code
}
});
答案 2 :(得分:0)
我复制了你的布局并粘贴在我的布局中。此外,我创建了一个活动,我再次粘贴了您的代码。在我的情况下,它工作正常:
公共类Activity1扩展AppCompatActivity实现了View.OnClickListener {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
Button button = (Button) findViewById(R.id.button);
Button button5 = (Button) findViewById(R.id.button5);
// this is the only part which I could not find in your code.
button.setOnClickListener(this);
button5.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if(view.getId() == R.id.button){
Intent intent = new Intent(Activity1.this, SplashActivity.class);
startActivity(intent);
}
else if(view.getId() == R.id.button5){
Intent intent = new Intent(Activity1.this, ActivityLifeCycle.class);
startActivity(intent);
}
}
}