好的,这是我的问题。我正在开发一个 android 应用程序并同时学习 android,所以大多数时候我都会出错。通常我可以在研究一下后修复它们,但我被困在这一点上。
我正在尝试为我的应用程序中的每个 Activity 制作一个后退按钮,所以我考虑制作一个“BackButton”类,这样我就可以在每次我想要的时候实例化它。这是我的后退按钮代码:
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
public class BackButton extends Activity implements View.OnClickListener{
public static Button BackButton;
// Defining the button
public BackButton() {
BackButton = (Button) findViewById(R.id.bBack);
BackButton.setOnClickListener(this);
}
//To get the Button
public static Button getBackButton() {
return BackButton;
}
// OnClickListener
public void onClick(View v) {
try {
Class MainActivityClass = Class.forName("eu.lafarga.treballderecerca.MainActivity");
Intent MainActivityIntent = new Intent(BackButton.this, MainActivityClass);
startActivity(MainActivityIntent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
// Save the things we've done.
}
}
}
那么,我应该如何在任何活动中实现这一点?我做错了什么?(当然我是哈哈)