I'm starting now in development for android. I'm trying to implement an onclicklistener on a button and it does not work at all, I've already followed step by step the video lessons from here, from youtube, and always from the error when compiling, what can it be? below is my java, xml, and logcat code:
Code
package com.example.treino;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
Button button;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
textView = (TextView) findViewById(R.id.textView1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("wow!");
}
});
}
}
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.treino.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="59dp"
android:text="Button" />
</RelativeLayout>
Log
05-26 19:22:33.703: D/AndroidRuntime(25773): Shutting down VM
05-26 19:22:33.703: W/dalvikvm(25773): threadid=1: thread exiting with uncaught exception (group=0x41f1dd40)
05-26 19:22:33.706: E/AndroidRuntime(25773): FATAL EXCEPTION: main
05-26 19:22:33.706: E/AndroidRuntime(25773): Process: com.example.treino, PID: 25773
05-26 19:22:33.706: E/AndroidRuntime(25773): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.treino/com.example.treino.MainActivity}: java.lang.NullPointerException
05-26 19:22:33.706: E/AndroidRuntime(25773): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
05-26 19:22:33.706: E/AndroidRuntime(25773): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
05-26 19:22:33.706: E/AndroidRuntime(25773): at android.app.ActivityThread.access$800(ActivityThread.java:139)
05-26 19:22:33.706: E/AndroidRuntime(25773): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
05-26 19:22:33.706: E/AndroidRuntime(25773): at android.os.Handler.dispatchMessage(Handler.java:102)
05-26 19:22:33.706: E/AndroidRuntime(25773): at android.os.Looper.loop(Looper.java:136)
05-26 19:22:33.706: E/AndroidRuntime(25773): at android.app.ActivityThread.main(ActivityThread.java:5102)
05-26 19:22:33.706: E/AndroidRuntime(25773): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 19:22:33.706: E/AndroidRuntime(25773): at java.lang.reflect.Method.invoke(Method.java:515)
05-26 19:22:33.706: E/AndroidRuntime(25773): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
05-26 19:22:33.706: E/AndroidRuntime(25773): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
05-26 19:22:33.706: E/AndroidRuntime(25773): at dalvik.system.NativeStart.main(Native Method)
05-26 19:22:33.706: E/AndroidRuntime(25773): Caused by: java.lang.NullPointerException
05-26 19:22:33.706: E/AndroidRuntime(25773): at com.example.treino.MainActivity.onCreate(MainActivity.java:28)
05-26 19:22:33.706: E/AndroidRuntime(25773): at android.app.Activity.performCreate(Activity.java:5248)
05-26 19:22:33.706: E/AndroidRuntime(25773): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
05-26 19:22:33.706: E/AndroidRuntime(25773): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
05-26 19:22:33.706: E/AndroidRuntime(25773): ... 11 more
05-26 19:22:35.598: I/Process(25773): Sending signal. PID: 25773 SIG: 9
05-26 19:25:13.048: D/AndroidRuntime(26448): Shutting down VM
05-26 19:25:42.291: D/AndroidRuntime(26858): Shutting down VM
05-26 19:25:42.291: W/dalvikvm(26858): threadid=1: thread exiting with uncaught exception (group=0x41f1dd40)
05-26 19:25:42.294: E/AndroidRuntime(26858): FATAL EXCEPTION: main
05-26 19:25:42.294: E/AndroidRuntime(26858): Process: com.example.treino, PID: 26858
05-26 19:25:42.294: E/AndroidRuntime(26858): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.treino/com.example.treino.MainActivity}: java.lang.NullPointerException
05-26 19:25:42.294: E/AndroidRuntime(26858): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
05-26 19:25:42.294: E/AndroidRuntime(26858): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
05-26 19:25:42.294: E/AndroidRuntime(26858): at android.app.ActivityThread.access$800(ActivityThread.java:139)
05-26 19:25:42.294: E/AndroidRuntime(26858): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
05-26 19:25:42.294: E/AndroidRuntime(26858): at android.os.Handler.dispatchMessage(Handler.java:102)
05-26 19:25:42.294: E/AndroidRuntime(26858): at android.os.Looper.loop(Looper.java:136)
05-26 19:25:42.294: E/AndroidRuntime(26858): at android.app.ActivityThread.main(ActivityThread.java:5102)
05-26 19:25:42.294: E/AndroidRuntime(26858): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 19:25:42.294: E/AndroidRuntime(26858): at java.lang.reflect.Method.invoke(Method.java:515)
05-26 19:25:42.294: E/AndroidRuntime(26858): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
05-26 19:25:42.294: E/AndroidRuntime(26858): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
05-26 19:25:42.294: E/AndroidRuntime(26858): at dalvik.system.NativeStart.main(Native Method)
05-26 19:25:42.294: E/AndroidRuntime(26858): Caused by: java.lang.NullPointerException
05-26 19:25:42.294: E/AndroidRuntime(26858): at com.example.treino.MainActivity.onCreate(MainActivity.java:23)
05-26 19:25:42.294: E/AndroidRuntime(26858): at android.app.Activity.performCreate(Activity.java:5248)
05-26 19:25:42.294: E/AndroidRuntime(26858): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
05-26 19:25:42.294: E/AndroidRuntime(26858): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
05-26 19:25:42.294: E/AndroidRuntime(26858): ... 11 more
05-26 19:25:43.807: I/Process(26858): Sending signal. PID: 26858 SIG: 9