How to connect between xml file and class? - java

I tried to call to a function that places in activity file. My xml file, trying to find this method in other activity file. How can I connect between both?
This is my activity file:
public class FirstScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi);
//Intent newActivity = new Intent(this, MainActivity.class);
//startActivity(newActivity);
}
public void gotomain(){
Intent newActivity = new Intent(this, MainActivity.class);
startActivity(newActivity);
}
}
This is my xml file (button onClick - search method in other class - what do I do here?):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_gravity="center_horizontal"
android:onClick=""/>
</LinearLayout>
Thanks a lot!

// Try this
Java Code
public void gotomain(View view){
Intent newActivity = new Intent(this, MainActivity.class);
startActivity(newActivity);
}
xml code.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_gravity="center_horizontal"
android:onClick="gotomain"/>

Related

Problem with ImageButton in Android Studio

I try to create two ImageButton in an activity but I can only see one of these. Someone can help me?
activity_ristorante.xml :
?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Ristoranti">
<ImageButton
android:id="#+id/b2"
style="#style/Widget.AppCompat.ImageButton"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginStart="232dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="30dp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/b1"
style="#style/Widget.AppCompat.ImageButton"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginStart="30dp"
android:layout_marginTop="50dp"
android:visibility="visible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Class:
public class Ristoranti extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton b1 = findViewById(R.id.b1);
ImageButton b2 = findViewById(R.id.b2);
b1.setBackgroundResource(R.drawable.putia) ;
b2.setBackgroundResource(R.drawable.pos);
b1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Uri uri = Uri.parse("https://maps.app.goo.gl/FoRGA2CbJdrV5cfT8");
Intent in = new Intent(Intent.ACTION_VIEW, uri);
startActivity(in);
}
});
what's errors? I can't see the button even though it's set to "visible" within android studioCan you help me? thank you
Just change setContentView(R.layout.activity_main); to setContentView(R.layout.activity_ristorante); or copy your XML code from activity_ristorante.xml to activity_main.xml

OnClickListener error: "[..] invoke virtual method [...] on a null object reference", athough string declared

I'm simply trying to use 2 buttons in my MainActivity. One opens a website (which works), the other should directly jump into my second class (which for simplification here, I only set as a second website). However, although button setup is similar to other button, using setOnClickListener as well as variable defined in resources, I'm receiving the error message below.
I already excluded several fragments - the error message seems to be concerning the button for accessing the 2nd class.
Apologies for getting lost with those trivialities...I'm lost but grateful for any hint.
java.lang.RuntimeException: Unable to start activity ComponentInfo{paperpad.app/paperpad.app.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
strings.xml
<resources>
<string name="logo">Logo</string>
<string name="app_name">PaperPad2</string>
<string name="button_go2activity">Start Drawing</string>
<string name="button_openPicture">Load Picture</string>
<string name="button_closePicture">Stop</string>
<string name="button_help">help</string>
<string name="websiteAddress">PaperPad.app</string>
</resources>
MainActivity.java
package paperpad.app;
import androidx.annotation.NonNull;
//etc.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button_openHelp = findViewById(R.id.button_openHelp);
button_openHelp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent openHelp = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com"));
startActivity(openHelp);
}
});
Button button_go2activity = findViewById(R.id.button_go2activity);
button_go2activity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent openLink = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(openLink);
}
});
}
}
my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
tools:context=".MainActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="254dp"
android:layout_height="237dp"
android:layout_marginTop="48dp"
android:layout_marginBottom="8dp"
android:contentDescription="#string/logo"
app:layout_constraintBottom_toTopOf="#+id/imageView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/logo_paperpad" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="59dp"
android:layout_marginTop="96dp"
android:layout_marginBottom="100dp"
app:layout_constraintBottom_toTopOf="#+id/button_openPicture"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView"
app:srcCompat="#drawable/logo_titel"
tools:ignore="ContentDescription" />
<Button
android:id="#+id/button_go2activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="269dp"
android:text="#string/button_go2activity"
android:textSize="24sp"
android:typeface="normal"
app:layout_constraintBottom_toTopOf="#+id/button_openHelp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2" />
<Button
android:id="#+id/button_openHelp"
android:layout_width="45dp"
android:layout_height="25dp"
android:layout_marginBottom="10dp"
android:background="#FFFFFF"
android:text="#string/button_help"
android:textColor="#android:color/black"
android:textSize="12sp"
app:layout_constraintBottom_toTopOf="#+id/linkWebsite"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_go2activity" />
<TextView
android:id="#+id/linkWebsite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:inputType="none"
android:text="#string/websiteAddress"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_openHelp" />
</androidx.constraintlayout.widget.ConstraintLayout>
Your button is not in the activity_main layout xml (currently). If in a fragment, it has not been loaded yet.
If it's defined in a fragment you should move this code to the fragment.
Try to declare Buttons on class level and initialize them later:
public class MainActivity extends AppCompatActivity {
Button button_openHelp;
Button button_go2activity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_openHelp = findViewById(R.id.button_openHelp);
button_openHelp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent openHelp = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com"));
startActivity(openHelp);
}
});
button_go2activity = findViewById(R.id.button_go2activity);
button_go2activity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent openLink = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(openLink);
}
});
}
}

Android Multiple Intents - One Form

Good afternoon,
I am trying to create a basic menu in my andorid application which contains 5 buttons each bringing you to another form. I am trying to create the java to carry out this action but appear to be running into the following error with each of my buttons
"EXAMPLE cannot be resolved as a variable"
Please help me in a solution to my code or if there is a simpler way to allow me to execute this menu with 5 buttons each going to a different form
<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.techblogon.loginexample.MainMenu" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/pic" />
<Button
android:id="#+id/btnFootball"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Football"
android:onClick="btnFootball" />
<Button
android:id="#+id/btnHockey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnFootball"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Hockey"
android:onClick="btnHockey" />
<Button
android:id="#+id/btnLacrosse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnLacrosse"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Lacrosse"
android:onClick="btnLacrosse" />
<Button
android:id="#+id/btnCurling"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnLacrosse"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Curling"
android:onClick="btnCurling" />
<Button
android:id="#+id/btnLogout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnCurling"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Logout"
android:onClick="btnLogout" />
</RelativeLayout>
Here is the Java:
package com.techblogon.loginexample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.content.Context;
import android.view.Menu;
import android.view.MenuItem;
public class MainMenu extends Activity {
public void ButtonOnClick(View v) {
switch (v.getId()) {
case R.id.btnFootball:
startActivity(Football);
break;
case R.id.btnHockey:
startActivity(Hockey);
break;
case R.id.btnLacrosse:
startActivity(Lacrosse);
break;
case R.id.btnCurling:
startActivity(Curling);
break;
case R.id.btnLogout:
startActivity(HomeActivity);
break;
}
}
}
#Meryl2
When u are using
""android:onClick="btnLogout"""
Then you should have corresponding method
public void btnLogout(View view{
//your code here
}
Same applies for all buttons in your code
Your Mistakes ->
You have not used setcontentView() nor you overridden onCreate() method. One more thing, you are not sending intent correctly.
Here is how you can send intent
Intent intent = new Intent(getApplicationContext(), DestinationActivity.class);
startActivity(intent);
I have modified the class and the layout. You need to create the other classes for each action.
public class MainActivity extends Activity implements Button.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onClick(View v) {
int id = v.getId();
Intent intent = null;
if(id == R.id.btnHockey) {
intent = new Intent(MainActivity.this, Hockey.class);
} else if(id == R.id.btnCurling) {
intent = new Intent(MainActivity.this, Curling.class);
} else if(id == R.id.btnFootball) {
intent = new Intent(MainActivity.this, Football.class);
} else if(id == R.id.btnLogout) {
intent = new Intent(MainActivity.this, Logout.class);
} else if(id == R.id.btnLacrosse) {
intent = new Intent(MainActivity.this, Lacrosse.class);
}
startActivity(intent);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<Button
android:id="#+id/btnFootball"
android:layout_below="#id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="Football"
android:onClick="onClick" />
<Button
android:id="#+id/btnHockey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnFootball"
android:layout_marginTop="25dp"
android:text="Hockey"
android:onClick="onClick" />
<Button
android:id="#+id/btnLacrosse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnHockey"
android:layout_marginTop="25dp"
android:text="Lacrosse"
android:onClick="onClick" />
<Button
android:id="#+id/btnCurling"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnLacrosse"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Curling"
android:onClick="onClick" />
<Button
android:id="#+id/btnLogout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnCurling"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Logout"
android:onClick="onClick" />
I think i got your problem. In each Button you set the name of the method that will be used when you click on it :
android:onClick="btnCurling"
android:onClick="btnHockey"
...
But you only have one method here which is :
public void ButtonOnClick(View v) {
...
}
One solution is to define each method like so :
public class MainMenu extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// here you tell your activity what layout he will display
setContentView(R.layout.TheNameOfYourLayout);
}
// it will get there when you click on the "#+id/btnHockey" Button
public void btnHockey(View v) {
Intent intent = new Intent(this, NameOfTheActivityToLaunch.class);
startActivity(intent);
}
// then you add the other ones btnCurling, ...
}
Another solution is to set the Listener of each button programmatically :
public class MainMenu extends Activity implements View.OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Here you tell your activity what layout he will display
setContentView(R.layout.TheNameOfYourLayout);
// This will find your button in the layout you just set
// and then set this Class as your Listener
findViewById(R.id.btnFootball).setOnClickListener(this);
// Then set the listener of all your other buttons
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnFootball:
Intent intent = new Intent(this, NameOfTheActivityToLaunch.class);
startActivity(intent);
break;
case R.id.btnHockey:
Intent intent = new Intent(this, NameOfTheActivityToLaunch.class);
startActivity(intent);
break;
// ...
}
}
}
You also have to remove all the android:onClick="btnCurling" line in your XML Layout file for this solution to work.
Hope it helps cheers :)
Try this code;
public class MainMenu extends Activity implements OnClickListener {
Button btn_football,btn_hokey,btn_something; // add another button here
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);
btn_football = (Button)findViewById(R.id.btnFootball);
// add another button here same way
btn_football.setOnClickListener(this);
// add another button here same way
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btnFootball:
Intent intent = new Intent(MainMenu.this,(nextclass_addhere).class);
startActivity(intent);
break;
case R.id.another button:
Intent intent = new Intent(MainMenu.this,(nextclass_addhere).class);
startActivity(intent);
break;
//.......add here another button with same way
}
}
}

Android intent to other Activity

I want to forward from current activity to other Activity. But when i click the button i am not able to perform it.I want to move from MainActivity to FeedActivity Please tell what is fault in my code.
<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.sample.test.MainActivity" >
<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_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginLeft="53dp"
android:layout_marginTop="92dp"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/button1"
android:text="#string/button2" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(),
"Welcome to android by Vivek", Toast.LENGTH_LONG)
.show();
}
});
btn2.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), FeedActivity.class);
startActivity(intent);
}
});
}
}
When you are creating a new instance of Intent, you need to give it two parameters:
current activity class instance
goto activity class instance
Also, you need to either import View library or use your new OnClickListener as new View.OnClickListener
You can do that by adding this to import:
import android.view.View;
import android.view.View.OnClickListener;
Change your btn2.onClickListener to :
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, FeedActivity.class);
startActivity(intent);
}

Android - View has no ID - why?

I have a class that is supposed to call another activity depending on which button has been clicked. This is done by checking the available IDs.
My problem is: The view that is passed to my class has no id, or rather, its value is NO_ID. What puzzles me is that the view, which is a button, does have an ID.
public class StrengthOrganizer extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_strength_organizer);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_strength_organizer, menu);
return true;
}
public void notifyActivity(View view) {
Intent intent = null;
int id = view.getId();
switch(id){
case R.id.button_log_book_launcher:
intent = new Intent(this, LogBook.class);
break;
case R.id.button_programming_launcher:
intent = new Intent(this, Programming.class);
break;
case R.id.button_visualizer_launcher:
intent = new Intent(this, Visualizer.class);
break;
default:
return;
}
startActivity(intent);
}
}
The corresponding XML file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical" >
<Button android:name="#+id/button_log_book_launcher"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:text="#string/button_log_book_launcher"
android:onClick="notifyActivity" />
<Button android:name="#+id/button_programming_launcher"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:text="#string/button_programming_launcher"
android:onClick="notifyActivity" />
<Button android:name="#+id/button_visualizer_launcher"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:text="#string/button_visualizer_launcher"
android:onClick="notifyActivity" />
</LinearLayout>
Why does the view object have no ID if it's been give in the XML file?
You used android:name instead of android:id in your XML to assign the ids. Use the latter instead.

Categories

Resources