I'm a new to Android, I'm trying to learn Android by some tutorials. Here is the error in logcat when I try to get start with button.I think the problem is these 2 buttons are not found by method findViewById. How can I fix it. error:
03-18 00:57:46.964: E/AndroidRuntime(1066): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cs656/com.example.cs656.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
and here is the activity
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button log_in;
private Button register;
private Button.OnClickListener Button_Listener = new Button.OnClickListener(){
public void onClick(View v){
setTitle("kkkkkkkkkkkk");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
log_in = (Button) findViewById(android.R.id.button1);
log_in.setOnClickListener(Button_Listener);
register = (Button) findViewById(android.R.id.button2);
}
}
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:baselineAligned="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id ="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register" />
<Button android:id ="@+id/log_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log In" />
</LinearLayout>