Beginner Error with Onclicklistener

Asked

Viewed 236 times

1

I’m starting now in development for android. I’m trying to implement an onclicklistener on a button and it doesn’t work at all, I’ve followed step by step the videos lessons here, youtube, and always the error when compiling, what can it be? below is my java code, xml and logcat:

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
  • You have a Nullpointerexception on line 37 of your class with.exampleandre.training2.MainActivity. If you can post your class code it is better.

  • When posting the class, be sure to inform which of the lines is the 37.

  • Pole also his activity_main.xml.

  • You could see there in your IDE and highlight lines 23 and 28 of Activity?

2 answers

1

Solved

As Piovezan said it was only to exchange activity_main for fragment_main in the method below:

setContentView(R.layout.activity_main);

0

I believe your mistake of NullPointerException be on the line where you have that statement : Button button1 = (Button)findViewById(R.id.button1);

You probably forgot to declare a button in your xml file with the name button1.

In this question /a/15440/8372 I explain about the button. In case you manage to solve your problem post here saying you got it.

  • Lucas! so dude, the hard that the boot is there in the xml layout with the msm name(id) right, and msm so ta giving error, so q I’m confused..!

  • @Andrecsimoes but the line that is giving error is this same one I spoke? You forgot to say which line 37.

  • Also forgot to post the res/layout/activity_main.xml.

  • Blz!! updated my code!

  • You have updated the code but it is not clear which XML file you posted. My guess is that it is not the res/layout/activity_main.xml and yes fragment_main.xml, which is the layout of Fragment that’s coming in the Hello World Eclipse currently. If so, you will have to move the lines of the onCreate() concerning the Button for the onCreateView() method of the class PlaceholderFragment (or, if you don’t want to use Fragments, move the contents of fragment_main.xml for activity_main.xml).

  • Piovezan! is fragment_main.xml msm, now I understand what you said to do, but I don’t know how to move the content of fragment_main.xml to activity_main.xml, I always felt that the two were the most thing.

  • Edit the question with your code.

  • @Andrecsimoes "I don’t know how to move the content of fragment_main.xml to activity_main.xml" - Copy & Paste, Ué. : ) The fragment layout is different from the Activity layout, the first can be included in the second as long as you take into account the life cycle of both, because at certain times the two do not get linked (including this was the cause of the error you had). When you study Fragments you will understand. To completely remove Fragments code from your Hello World, follow those instructions.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.