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" />
@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
– Ivan Silva
The
button
is inR.layout.activity_main
? Usually this happens when thefindViewById()
returnnull
– Ivan Silva
The button is in a layout Fragment
– iagoth
To
NullPointerException
is usually launched when thefindViewById
cannot find the view. Move your button to thelayout
ofActivity
or the logic for theFragment
– Ivan Silva
I don’t understand what you mean by layout ! and the res folder ?
– iagoth
The
layout
is the filexml
that represents your UI (interface), in your case, thelayout
ofActivity
is the fileres/layout/content_main
– Ivan Silva
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 ?
– iagoth
puts the code of your Fragment, if the button is in Fragment, you need to call him there
– Murillo Comino
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 !
– iagoth
I decided it was how Ivan had explained ! I just needed to create an interface to manage the screen change ! Obg by orientation !
– iagoth