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.
Related
I'm developing an Android app on my free time to learn about Android development. I'm trying to make a Grade/GPA Calculator App. I currently have a button called "+ New Semester" whose purpose is to open a popup where the user inputs the semester name. This can be seen in the following two images:
User clicks on "+ New Semester" and the popup appears prompting the user to add the name of that semester.
Now, what I want to do is that when the user clicks on the "Done" button a new button with the text that the user typed in into the text box is created below the "+ New Semester" button, but I can't figure out how to do it. I would appreciate any help.
This is the code I currently have:
package com.example.gradecalculator;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// Private Fields
private Dialog d;
private ImageButton newSemesterButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
d = new Dialog(this);
}
// When user clicks on "+ New Semester" button open a popup where the user is prompted to
// type in the Semester Name and when "Done" is clicked the new semester appears in the view
public void newSemesterPopup(View v) {
TextView closePopup;
ImageButton doneButton;
d.setContentView(R.layout.new_semester_popup);
doneButton = (ImageButton) d.findViewById(R.id.doneButton);
doneButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openMainActivity();
}
});
closePopup = (TextView) d.findViewById(R.id.exitButton);
closePopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
d.dismiss();
}
});
d.show();
}
// Open Main Activity
public void openMainActivity() {
Intent main = new Intent(this, MainActivity.class);
startActivity(main);
}
}
You can achieve that by first getting the reference to the layout where you want to add your button. Lets say, the name of the layout where your button should be has the name btn_layout,
Bind the layout. (i.e. LinearLayout layout = findViewById (R.id.btn_layout); )
When the DONE button is clicked, create your new button and add it to the layout. For example:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setText("Your text from edittext");
layout.addView(btn, params);
Even better if you can declare the Button and Layout as fields
You have to get the reference from your buttons parent layout. then create a button and add it to the view. Something like this:
public void newSemesterPopup(View v) {
TextView closePopup;
ImageButton doneButton;
d.setContentView(R.layout.new_semester_popup);
doneButton = (ImageButton) d.findViewById(R.id.doneButton);
doneButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openMainActivity();
}
});
closePopup = (TextView) d.findViewById(R.id.exitButton);
closePopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myParentLayout = (LinearLayout) findViewById(R.id.myLinearLayout);
params = (LinearLayout.LayoutParams) new
LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
yourEditText = d.findViewById(R.id.YourEditText);
myNewButton = new Button(this);
String buttonText = yourEditText.text.toString();
myNewButton.setText(buttonText);
myParentLayout.addView(myNewButton, params);
d.dismiss();
}
});
d.show();
}
Just make that new button on main screen and set visibility to false = newButton.setVisibility(false);
and if you press button done on popup just set visibility to true hope that will help, not sure if that's what you mean
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);
}
});}}
I'm new to android develop. i just stated this week.
i dont know if im asking stupid question but i want to learn.
i have created an app just click a button change the background color.
my layout is this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.seluhadu.colorpicker.MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="136dp"
tools:layout_editor_absoluteY="1dp"
android:elevation="8dp"
android:layout_alignBaseline="#+id/button3"
android:layout_alignBottom="#+id/button3"
android:layout_alignParentEnd="true"
android:layout_marginEnd="52dp"/>
</RelativeLayout>
and i have a class "java" like this
public class MainActivity extends AppCompatActivity {
Button button;
RelativeLayout relativeLayout;
Integer[] colors = {
R.color.color1,
R.color.color2,
R.color.color3,
R.color.color4,
R.color.color5,
R.color.color6,
R.color.color7,
R.color.color8,
R.color.color9,
R.color.color10,
R.color.color11,
R.color.color12,
R.color.color13,
R.color.color14,
R.color.color15,
R.color.color16,
R.color.color17,
R.color.color18,
R.color.color19,
R.color.color20
};
Random random;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
random = new Random();
relativeLayout = (RelativeLayout) findViewById(R.id.background);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
relativeLayout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),
colors[random.nextInt(colors.length)]));
}
});
}
it just change the background color random.
when it change the color if i rotat the screen it change to default back.
my question is how to make a onSaveInstanceState and onRestoreInstanceState method to save the change color even if i reopen the app to stay the color that i change.
OR how can i do like that in other way?
thank you for your help!
When your activity is recreated after it was previously destroyed, you can recover your saved state from the Bundle that the system
passes your activity. Both the onCreate() and onRestoreInstanceState()
callback methods receive the same Bundle that contains the instance
state information.
Because the onCreate() method is called whether the system is creating
a new instance of your activity or recreating a previous one, you must
check whether the state Bundle is null before you attempt to read it.
If it is null, then the system is creating a new instance of the
activity, instead of restoring a previous one that was destroyed.
public class MainActivity extends AppCompatActivity {
Button button;
RelativeLayout relativeLayout;
Integer[] colors = {
R.color.color1,
R.color.color2,
R.color.color3,
R.color.color4,
R.color.color5,
R.color.color6,
R.color.color7,
R.color.color8,
R.color.color9,
R.color.color10,
R.color.color11,
R.color.color12,
R.color.color13,
R.color.color14,
R.color.color15,
R.color.color16,
R.color.color17,
R.color.color18,
R.color.color19,
R.color.color20
};
Random random;
Boolean backgroundChanged = false;
int colorIndex;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore background color from saved state
backgroundChanged = savedInstanceState.getBoolean("backgroundChanged", false);
colorIndex = savedInstanceState.getInt(colorIndex, 0);
}
random = new Random();
relativeLayout = (RelativeLayout) findViewById(R.id.background);
if(backgroundChanged) {
relativeLayout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), colors[colorIndex]));
}
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
backgroundChanged = true;
colorIndex = random.nextInt(colors.length);
relativeLayout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), colors[colorIndex]));
}
});
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putBoolean("backgroundChanged", backgroundChanged);
if(backgroundChanged) {
savedInstanceState.putInt("colorIndex", colorIndex);
}
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
Update
Here's my code below, plugged into your posted example:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
random = new Random();
relativeLayout = (RelativeLayout) findViewById(R.id.background);
button = (Button) findViewById(R.id.button);
SharedPreferences settings = getSharedPreferences("myPrefs", 0);
int colorIndex = settings.getInt("colorIndex", 0);
updateBackground(colorIndex);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int colorIndex = random.nextInt(colors.length);
SharedPreferences settings = getSharedPreferences("myPrefs", 0);
settings.edit().putInt("colorIndex", colorIndex).apply();
updateBackground(colorIndex);
}
});
}
private void updateBackground(int colorIndex) {
relativeLayout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), colors[colorIndex]));
}
If you have to store the color even through app restarts, you should use SharedPreferences instead of using onSaveInstanceState().
In your onClick(), when you generate the random index for your color array, write that value to your SharedPreferences object. In onCreate() when you set everything up, read the value out of the SharedPreferences.
To write:
int colorIndex = random.nextInt(colors.length);
SharedPreferences settings = getSharedPreferences("myPrefs", 0);
settings.edit().putInt("colorIndex", colorIndex).apply();
To read:
SharedPreferences settings = getSharedPreferences("myPrefs", 0);
int colorIndex = settings.getInt("colorIndex", 0);
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~!
I have the following Java code for my button event. This code doesn't generate any error at compile-time, but the app crashes during launch.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b0 = (Button) findViewById(R.id.button0);
b0.setOnClickListener(this);
}
String t = "";
TextView inp = (TextView) findViewById(R.id.input);
public void onClick(View v)
{
if (v == findViewById(R.id.button0))
{
t = "0";
inp.setText(inp.getText()+t);
}
}
The Xml Code is :
<Button
android:id="#+id/button0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/dbtn"
android:layout_alignBottom="#+id/dbtn"
android:layout_toLeftOf="#+id/button3"
android:text="0" />
What am I doing wrong?
TextView inp = (TextView) findViewById(R.id.input);
Put the above line inside onCreate() method after you call setContentView() and change you TextView variable scope from local to global.
This should fix the issue. If you still having trouble, let me know.