Error when routinely attaching to button

Asked

Viewed 40 times

0

Good afternoon I’m trying to get a button to open a new app but when I run the app it won’t open.

Knob

<Button
        android:id="@+id/solare"
        android:layout_width="83dp"
        android:layout_height="83dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="69dp"
        android:layout_marginTop="167dp"
        android:layout_marginEnd="254dp"
        android:layout_marginBottom="358dp"
        android:background="@drawable/solo"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.153"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.263" />

inserir a descrição da imagem aqui

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.addDrawerListener(toggle);
            toggle.syncState();

            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);


            button =(Button)findViewById(R.id.solare);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    openActivity2();
                }
            });

        }

        public void openActivity2(){
            Intent itent = new Intent(this,Solo.class);
            startActivity(itent);
        }

` Here I try to call him

THAT AND THE FRAGMENT WHERE MY BUTTON IS HERE AND THE FRAGMENT ACTIVITY HERE AND THE MAIN CLASS WHERE THE MENU OF MY APP IS AND WHERE I WAS TRYING TO CALL THE BUTTON]4

  • Hello, Iago! Add a more descriptive title to your question

  • The button is in R.layout.activity_main? Usually this happens when the findViewById() return null

  • The button is in a layout Fragment

  • To NullPointerException is usually launched when the findViewById cannot find the view. Move your button to the layout of Activity or the logic for the Fragment

  • I don’t understand what you mean by layout ! and the res folder ?

  • The layout is the file xml that represents your UI (interface), in your case, the layout of Activity is the file res/layout/content_main

  • It will be very extensive but.. the button is on Activity main ! I would have to put on Activity that I’m trying to call ?

  • puts the code of your Fragment, if the button is in Fragment, you need to call him there

  • I put the code of Fragment ! I tried to implement the tip of Ivan but not right , I do not know if I did wrong !

  • I decided it was how Ivan had explained ! I just needed to create an interface to manage the screen change ! Obg by orientation !

Show 5 more comments

1 answer

1


Come on, Iago, come on!

Avoiding an extensive discussion in the comments, I will try in a reply, embed two solutions.

In a first scenario, where the Button is located in the Fragment like you said

The button is in a layout Fragment

And you’re calling the Button in Acitvity, you will probably receive an error of NullPointerException, because the button does not exist in the layout of Activity. To resolve this, move the button to where it should be accessed, on Fragment

public class NomeDoSeuFragment extends Fragment implements View.OnClickListener {
    private Button mSeuButton;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_do_seu_fragment, container, false);

        /* ... */
        mSeuButton = view.findViewById(R.id.solares);
        return view;
    }

    @Override
    public void onClick(View v) {
       if(v.getId() == R.id.solares) {
          startActivity(new Intent(getAcivity(), ActivityDeDestino.class));
       }
    }
}

In a second scenario, where you want to perform these actions in the Activity, just move your button to the layout of Activity (Those layouts are files xml representing the elements of its interface); O layout is located in res/layout/content_main,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
    android:id="@+id/solare"
    android:layout_width="83dp"
    android:layout_height="83dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="69dp"
    android:layout_marginTop="167dp"
    android:layout_marginEnd="254dp"
    android:layout_marginBottom="358dp"
    android:background="@drawable/solo"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.153"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.263"
    />

</LinearLayout>

And then just follow how you were doing, calling the button on Activity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.addDrawerListener(toggle);
            toggle.syncState();

            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);


            button = findViewById(R.id.solare);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(this, ActivityDeDestino.class));
                }
            });

        }

In your case, I believe the first solution is the most appropriate one. I hope it’s made clear. Hugs!

Browser other questions tagged

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