さて、ここに私の問題があります。私はAndroidアプリに取り組んでおり、同時にAndroidを学習しているため、ほとんどの場合エラーが発生します。通常、少し調査すれば修正できますが、この点で行き詰まっています。
アプリのすべてのアクティビティに戻るボタンを作成しようとしているので、いつでもインスタンス化できるように「BackButton」クラスを作成することを考えました。ここに私の 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.
}
}
}
では、これをアクティビティにどのように実装すればよいでしょうか? 私は何か間違ったことをしていますか?(もちろん私です笑)