I am trying to make a button on my homepage of an app that will lead to a search page, that will have a handful more buttons leading to other pages. However, I used the same code from my activity main for the button in my second page (seachpage) and now when I run the code, my first button on my main page, when clicked it just shuts the app down. I don't even know how to approach this properly because I copied the same code, just changed the "findViewById" and the "startActivity" accordingly with their new labels. Any recommendation or help would be massively appreciated!
Activity main Java code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button yourButton = (Button) findViewById(R.id.TranslateButton);
if (yourButton == null) throw new AssertionError();
yourButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SearchPage.class));
}
});
}
}
Activity main xml for the button:
Secondary page (searchpage) Java code:
public class SearchPage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_page);
Button accommodationButton = (Button) findViewById(R.id.accommodationButton);
if (accommodationButton == null) throw new AssertionError();
accommodationButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(SearchPage.this, Accommodation.class));
}
});
}
}
Secondary page xml for the button:
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="#string/accommodation"
android:id="#+id/accommodationButton"
android:layout_below="#+id/search_bar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_weight="1"/>
Thank you again for taking the time and consideration to read and/or respond to my question~!
Related
Can u help me understand why when I want set text in TextView, I got NullPointerException? I know, because my TextView is null but how I can get TextView again?
What is my logic:
Start Application
Click button
Go to nullPoint and get data from firebase, when the proccessing is finished back to MainActivity and update text in TextView.
This is my example code:
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnAddNewWord = findViewById(R.id.button);
btnAddNewWord.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
nullPoint np = new nullPoint();
np.takeCount();
}
});
}
public void setTextView(int count){
TextView tv = findViewById(R.id.textView);
tv.setText("count = " + count);
}
}
nullPoint
public class nullPoint {
//the class gets asynchronous data from the Firebase database and does not know when the process will end
public void takeCount(){
//det data
//.
//.
// finish
//send data to MainActivity and update textView text
MainActivity ma = new MainActivity();
ma.setTextView(5);
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity"
android:layout_gravity="center">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_gravity="center"/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="center"/>
</LinearLayout>
I guess the nullPoint class is not inside the MainActivity. As a result, inside your nullPoint class there is no reference for the TextView in which you are trying to set a data.
In your case I would like to suggest you to implement a listener like the following.
public interface FirebaseResponseListener {
void onFirebaseResponseReceived(int count);
}
Now from your MainActivity, you need to implement the listener as follows.
public class MainActivity extends AppCompatActivity implements FirebaseResponseListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnAddNewWord = findViewById(R.id.button);
btnAddNewWord.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
nullPoint np = new nullPoint();
np.listener = this; // Initialize the listener here
np.takeCount();
}
});
}
public void setTextView(int count){
TextView tv = findViewById(R.id.textView);
tv.setText("count = " + count);
}
#Override
void onFirebaseResponseReceived(int count) {
setTextView(count);
}
}
And you have to add a listener in your nullPoint class as well.
public class nullPoint {
public FirebaseResponseListener listener;
//the class gets asynchronous data from the Firebase database and does not know when the process will end
public void takeCount(){
//det data
//.
//.
// finish
//send data to MainActivity and update textView text
listener.onFirebaseResponseReceived(5);
}
}
Hope that solves your problem.
I have the following piece of codes.I am calling a second activity from main activity.Whenever the send button is pressed i want a toast to show button is pressed and start the activity.But due to some context problems only toast is appearing.Please correct the context for intent and give some clear explaination about these contexts.
MainActivity.java
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE="com.example.iamka.androiddevelop.MESSAGE";
public void Toast1(String s){
Toast.makeText(this,s+" is called",Toast.LENGTH_SHORT).show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("My app","onCreate is called");
Toast1("onCreate");
Button btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("My app","Button is pressed");
Toast.makeText(MainActivity.this,"Button pressed",Toast.LENGTH_SHORT).show();
}
});
}
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
Log.i("intent","intent is started");
startActivity(intent);
}
}
DisplayMessageActivity.java
public class DisplayMessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView =(TextView) findViewById(R.id.textView);
Log.i("intent","displaymessage");
textView.setText(message);
}
}
activity_main.xml
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="#string/edit_message"
android:inputType="textPersonName"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toLeftOf="#+id/button"
app:layout_constraintHorizontal_chainStyle="spread" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:onClick="sendMessage"
android:text="#string/button_send"
app:layout_constraintBaseline_toBaselineOf="#+id/editText"
app:layout_constraintLeft_toRightOf="#+id/editText"
app:layout_constraintRight_toRightOf="parent" />
activity_display_message.xml
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="TextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
When i remove the onClickListener() method then the intent is working.
Since you are setting your own View.OnClickListener, you are removing the one from the XML definition. Button only support one View.OnClickListener. First, the XML will create one from the android:onclick attribute like :
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sendMessage(v);
}
});
Then you are setting yours with the Toast. The button will only keep the last one, so the Intent is never send.
Solutions :
call the sendMessage method in your listener
Like:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("My app","Button is pressed");
Toast.makeText(MainActivity.this,"Button pressed",Toast.LENGTH_SHORT).show();
sendMessage(v); //Or anywhere in that method, your call.
}
});
remove the listener to keep the one create by the android:onclick.
FYI:
Usually, a set### methods means this is not supporting multiple values, add### methods do.
Also, you can check at android- multi onClick listener in one button to implement your own multi listener button if you like. But I didn't check if there were some more up to date...
Just remove android:onClick="sendMessage" for button and try. Either you have to set click listener in xml or in class file. Both it won't work
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:text="#string/button_send"
app:layout_constraintBaseline_toBaselineOf="#+id/editText"
app:layout_constraintLeft_toRightOf="#+id/editText"
app:layout_constraintRight_toRightOf="parent" />
Change this in your Activity :
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("My app","Button is pressed");
Toast.makeText(MainActivity.this,"Button pressed",Toast.LENGTH_SHORT).show();
sendMessage();
}
});
private void sendMessage() {
// Do something in response to button
Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
Log.i("intent","intent is started");
startActivity(intent);
}
Just copy paste the following MainActivity code and it will do the work. The xmls are loaded first and then on runtime you are setting the onclick listener to the button again. so the xmls onclick has been replaced by your onclick listener in java code. Hence you get toast but the sendMessage() is never called
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE="com.example.iamka.androiddevelop.MESSAGE";
public void Toast1(String s){
Toast.makeText(this,s+" is called",Toast.LENGTH_SHORT).show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("My app","onCreate is called");
Toast1("onCreate");
Button btn=(Button)findViewById(R.id.button);
}
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
Log.i("intent","intent is started");
startActivity(intent);
}
}
My problem is the following.
my app has a 1 welcome screen where the user ckick the "continue" button and it goes to next screen. The next one contains a menu with several buttons.
my problem is that I can not open another activity on the second screen (on the first screen it opens normal)
more or less this scheme below
(| activity1> button continue | >> | activity2> button continue2 |> does not respond)
to compliment and test apk on a galaxy grand duos 4.2.2
code below
code 1 screen (welcome).
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Button button7 = (Button) findViewById(R.id.button7);
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.activity_main4);
}
});}}
code 2 tela
public class Main4Activity extends AppCompatActivity {
private Button prova;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
prova = (Button) findViewById(R.id.button5);
prova.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent( Main4Activity.this, Main3Activity.class);
startActivity(intent);
}
});
}}
2 tela code xml button
<Button
android:id="#+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button4"
android:layout_marginTop="11dp"
android:text="tela 2"/>
First, I want to make sure that you understand what you are writing.
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.activity_main4);
}
});
In your onclick, you only set your view of Main2Activity to screen of activity_main4.xml. But you are still standing in Main2Activity (It means you are in Main2Activity with view activity_main4.xml).
In this case, Main4Activity hasn't initialized and the button prova hasn't been initialized too. So when you press prova button, it won't do anything.
Second, to solve your problem, make Main4Activity be initialized, you must start it. So, instead of using:
setContentView(R.layout.activity_main4);
in Main2Activity, which only change the view, not the Activity. You should use
Intent intent = new Intent(Main2Activity.this, Main4Activity.class);
startActivity(intent);
Hope you can understand this!
Your problem is that you can't use setContentView(R.layout.activity_main4); to open another activity .You can use startActivity method to open another activity .
You can try this .
1.remove the code in your Main2Activity
setContentView(R.layout.activity_main4);
2.add change to this
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i=new Intent(Main2Activity.this,Main4Activity.this);
startActivity(i);
}
});}}
Hi guys I have a problem regarding the android dialog box.What I am trying to do is set few lines of description in a dialog box which I am able to do it fine but at last i need a link called "see more" which will redirect the user to other activity.I am very new to android and these is the first of some things what I am trying to do any help will be appreciated..my code onStart()
protected void onStart()
{
super.onStart();
/*final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.main);
dialog.setTitle("About Service One");
Button button = (Button) dialog.findViewById(R.id.button12);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
protected void onStart()
{
super.onStart();
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.main);
dialog.setTitle("About Service One");
Button button = (Button) dialog.findViewById(R.id.button12);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
TextView showMore= (TextView ) dialog.findViewById(R.id.tvShowMore);
showMore.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(getApplicationContext(),ShowMoreActivity.class);
startActivity(intent);
}
});
dialog.show();
}
And add in xml showmore TextView Which set text from string.xml
i.e <string name="show_more"><u>Show More </u></string> use this for set linkable text
main.xml
...........
<TextView
android:id="#+id/tvShowMore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/show_more"
android:textAppearance="?android:attr/textAppearanceMedium"
>
</TextView>
............
Create a custom Dialod layout>> create a text view as show more >> set OnClick Listener on it as ..
You can set the click handler in xml with these attribute:
android:onClick="onClick"
android:clickable="true"
Don't forget the clickable attribute, without it, the click handler isn't called.
dailog.xml
...
<TextView
android:id="#+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show more"
android:textSize="55sp"
android:onClick="onClick"
android:clickable="true"/>
...
MyActivity.java
public class MyActivity extends Activity {
public void onClick(View v) {
// ... start show more activity here
}
}
OR... find the view using dialog and set OnClick Listener on it..
showmore = (TextView)dialog.findViewById(R.id.click);
showmore..setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// ... start show more activity here
}
});
Try this:
<TextView
android:id="#+id/yourId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show more"
android:onClick="showMore"
android:clickable="true"/>
Inside your activity, get that textView :
TextView showMoreView=(TextView)findViewById(R.id.yourId);
Add this showMoreView inside your text in the dialog and
Inside your activity, define the methode "showMore"
public void showMore(View view)
{
Intent intent =new Intent(YourCurrrentActivity.this,NextActivity.class);
startActivity(intent);
}
This will take you to NextActivity on clicking the "show more" text.
So when I try and run my code in the emulator, the app background pops up then closes giving me the dialog, "Unfortunately, Callisto has stopped working"
I have no idea what is wrong other than it gives me a null pointer exception (line 49) but there is nothing at line 49
XML
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/callisto_heading" />
<Button
android:id="#+id/bClasses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Classes"
android:layout_gravity="center"
android:onClick=""
/>
<Button
android:id="#+id/bSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
android:layout_gravity="center"
android:onClick=""
/>
</LinearLayout>
Java
package android.callisto.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
public class CallistoActivity extends Activity {
/** Called when the activity is first created. */
Button settings_button;
Button classes_button;
Button home_button;
CheckBox notif_cb;
CheckBox math_cb;
CheckBox science_cb;
CheckBox ss_cb;
CheckBox english_cb;
CheckBox language_cb;
boolean notif,math,science,english,ss,language;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//settings layout
notif_cb = (CheckBox) findViewById(R.id.cbNotif);
math_cb = (CheckBox) findViewById(R.id.cbMath);
science_cb = (CheckBox) findViewById(R.id.cbScience);
ss_cb = (CheckBox) findViewById(R.id.cbSS);
english_cb = (CheckBox) findViewById(R.id.cbEnglish);
language_cb = (CheckBox) findViewById(R.id.cbLang);
home_button = (Button) findViewById(R.id.bHome);
notif = true;
math = true;
science = true;
english = true;
ss = true;
language = true;
home_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.main);
}
});
//notifications
//main layout
settings_button = (Button) findViewById(R.id.bSettings);
classes_button = (Button) findViewById(R.id.bClasses);
settings_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.settings);
}
});
}
}
FYI the app was loading last night.. Thanks for any help and please remember I am new to Android programming. Thank you.
the problem is here
home_button = (Button) findViewById(R.id.bHome);
in layout file there is no bHome
The problem probabaly is, that you are using the setContentView() for displaying different screens. You should not use this method for changing screens,you should use a different activitys for this.
Usually, you only set the contentView ONCE in the oncreate, per activity.
Your R.layout.main layout probabaly does not contain the 'home' button, but the r.layout.settings layout does.
First you are loading the main layout at this line: setContentView(R.layout.main); But since this layout file does NOT contain the home button, findViewById(R.id.bHome); will return null. After that, calling a method on this returned value, home_button.setOnClickListener(); in your case, will cause a NullPointerException.
What you should do is the following:
Create an activity for your main layout:
public class CallistoMainActivity extends Activity {
/** Called when the activity is first created. */
Button settings_button;
Button classes_button;
Button home_button;
CheckBox notif_cb;
CheckBox math_cb;
CheckBox science_cb;
CheckBox ss_cb;
CheckBox english_cb;
CheckBox language_cb;
boolean notif,math,science,english,ss,language;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//main layout
settings_button = (Button) findViewById(R.id.bSettings);
classes_button = (Button) findViewById(R.id.bClasses);
settings_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(getApplicationContext(),
CallistoSettingsActivity.class));
}
});
}
And an activity for your Settings layout:
public class CallistoSettingsActivity extends Activity {
Button home_button;
CheckBox notif_cb;
CheckBox math_cb;
CheckBox science_cb;
CheckBox ss_cb;
CheckBox english_cb;
CheckBox language_cb;
boolean notif,math,science,english,ss,language;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
//settings layout
notif_cb = (CheckBox) findViewById(R.id.cbNotif);
math_cb = (CheckBox) findViewById(R.id.cbMath);
science_cb = (CheckBox) findViewById(R.id.cbScience);
ss_cb = (CheckBox) findViewById(R.id.cbSS);
english_cb = (CheckBox) findViewById(R.id.cbEnglish);
language_cb = (CheckBox) findViewById(R.id.cbLang);
home_button = (Button) findViewById(R.id.bHome);
notif = true;
math = true;
science = true;
english = true;
ss = true;
language = true;
home_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
//You could either use finish() to finish the current activity, and return to the previous CallistoMainActivity on the stack.
//Or you could simply start the main activity again by using the startActivity() method you see in the onclicklistener on the settings button.
}
});
}
What happens now, is when you click the settings button in the main activity, the settings activity will be shown. When you click the home button in the settings activity, the settings activity will be finished, and the user will be returned to the previous activity on the stack; which is the main activity.
Also do not forget to define the settingsactivity in your AndroidManifest.xml
EDIT: Another thing you should note, that if there is an error 'at line x' but there is nothing at line x in your code, then the code running on your Android device, is not the same as the code you are looking at in your editor. So that might be the reason it was running last night, but not anymore.