Caused by: java.lang.OutOfMemoryError - java

I am having a layout file and 5 buttons -- 1 for next and 2 for previous etc. Application runs fine, when I click first button, it loads the image correctly, then as I click another button, it gives the error -Caused by java lang Out Of Memory Error
public class MainActivity extends ActionBarActivity {
private ShareActionProvider mShareActionProvider;
ImageButton btn1;
ImageButton btn2;
ImageButton btn3;
ImageButton btn4;
ImageButton btn5;
ImageButton btn6;
ImageButton btn7;
ImageButton btn8;
ImageView image1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
addListenerOnButton2();
addListenerOnButton3();
addListenerOnButton4();
addListenerOnButton5();
addListenerOnButton6();
}
public void addListenerOnButton() {
btn1= (ImageButton) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.show);
}
}
);
}
public void addListenerOnButton2() {
btn2= (ImageButton) findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k2);
}
}
);
}
public void addListenerOnButton3() {
final Context context =this;
btn3= (ImageButton) findViewById(R.id.btn3);
btn3.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k3);
}
}
);
}
public void addListenerOnButton4() {
final Context context =this;
btn4= (ImageButton) findViewById(R.id.btn4);
btn4.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k4);
}
}
);
}
public void addListenerOnButton5() {
final Context context =this;
btn5= (ImageButton) findViewById(R.id.btn5);
btn5.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k5);
}
}
);
}
public void addListenerOnButton6() {
final Context context =this;
btn6= (ImageButton) findViewById(R.id.btn6);
btn6.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k6);
}
}
);
}

You are changing the whole layout instead of changing the image. When you do that, garbage collector is not called and you should be calling it on your own. Since it's not called, you are going to get that error pretty soon.
Do it like this instead
int[] imageArray = {R.drawable.image1, R.drawable.image2, ... };
and when you click a button, just do
btn1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
someImageView.setImageResource(imgageArray[0])
}
}
Also make sure you don't use too big images. The take up a lot of memory.

Related

setOnClickListener button doesn't work Android studio

I'm new in java and I have tried to make a food app in android studio. I have a bar in app and when I click on cart my app is crashing.
Here is my MainActivity
public class MainActivity extends AppCompatActivity {
private RecyclerView.Adapter adapter, adapter2;
private RecyclerView recyclerViewCategotyList, recyclerViewPopularList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerViewCategoty();
recyclerViewPopular();
bottomNavigation();
}
private void bottomNavigation() {
LinearLayout homeBtn=findViewById(R.id.homeBtn);
LinearLayout cartBtn=findViewById(R.id.cartBtn);
homeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,MainActivity.class));
}
});
cartBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,CartActivity.class));
}
});
}
And I have this error: at com.example.food.Activity.MainActivity$2.onClick(MainActivity.java:48)
This is CartActivity
public class CartActivity extends AppCompatActivity {
private RecyclerView.Adapter adapter;
private RecyclerView recyclerViewList;
private ManagementCart managementCart;
private TextView totalFeeTxt, taxTxt, deliveryTxt, totalTxt, emptyTxt;
private double tax;
private ScrollView scrollView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
managementCart = new ManagementCart(this);
initView();
initList();
bottomNavigation();
calculateCard();
}
private void bottomNavigation() {
LinearLayout homeBtn = findViewById(R.id.homeBtn);
LinearLayout cartBtn = findViewById(R.id.cartBtn);
homeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(CartActivity.this, MainActivity.class));
}
});
cartBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(CartActivity.this, MainActivity.class));
}
});
}
I don't know what's wrong and why my setOnClickListener doesn't work.
Thank you.
I think your button R.id.homeBtn is not LinearLayout, can you show your xml file?
If your buttons are Button classes, try this
private void bottomNavigation() {
Button homeBtn = findViewById(R.id.homeBtn);
Button cartBtn = findViewById(R.id.cartBtn);
homeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(CartActivity.this, MainActivity.class));
}
});
cartBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(CartActivity.this, MainActivity.class));
}
});
}

How to access array of drawables from a method

I have researched similar problems but none is a fit for this problem. Am trying to access the arrays of drawables from the method name called nextQuestion(), but i keep getting the error cannot resolve symbol 'ballArray' any help please. I just feel that this should work but don't know why. Here is my code.
public class MainActivity extends AppCompatActivity {
ImageView mBallDisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int quote;
mBallDisplay = (ImageView) findViewById(R.id.image_eightBall);
Button nextbutton = (Button) findViewById(R.id.nextbutton);
Button prevbutton = (Button) findViewById(R.id.prevbutton);
final int[] ballArray = {
R.drawable.ball1,
R.drawable.ball2,
R.drawable.ball3,
R.drawable.ball4,
R.drawable.ball5
};
//next button
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextQuestion();
}
});
//previous button
prevbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prevQuestion();
}
});
}
private void nextQuestion(){
mBallDisplay.setImageResource(ballArray[4]);
}
private void prevQuestion(){
}
}
The reason you can't access ballArray from within nextQuestion() is because it's declared in a separate method. If you want it accessible from within nextQuestion(), you would need to make it visible at that level:
ImageView mBallDisplay;
final int[] ballArray = {
R.drawable.ball1,
R.drawable.ball2,
R.drawable.ball3,
R.drawable.ball4,
R.drawable.ball5
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int quote;
mBallDisplay = (ImageView) findViewById(R.id.image_eightBall);
Button nextbutton = (Button) findViewById(R.id.nextbutton);
Button prevbutton = (Button) findViewById(R.id.prevbutton);
//next button
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextQuestion();
}
});
//previous button
prevbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prevQuestion();
}
});
}
private void nextQuestion(){
mBallDisplay.setImageResource(ballArray[4]);
}
private void prevQuestion(){
}
}
Do like this:-
public class MainActivity extends AppCompatActivity {
ImageView mBallDisplay;
int[] ballArray = {
R.drawable.ball1,
R.drawable.ball2,
R.drawable.ball3,
R.drawable.ball4,
R.drawable.ball5
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int quote;
mBallDisplay = (ImageView) findViewById(R.id.image_eightBall);
Button nextbutton = (Button) findViewById(R.id.nextbutton);
Button prevbutton = (Button) findViewById(R.id.prevbutton);
//next button
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextQuestion();
}
});
//previous button
prevbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prevQuestion();
}
});
}
private void nextQuestion(){
mBallDisplay.setImageResource(ballArray[4]);
}
private void prevQuestion(){
}
}

Android :- Separate class for handling views clicks etc;

I am new to development field. I am developing android application where in my activity_main layout I have many different items. On clicking one of the button the top layout is replaced with a new layout.
Now instead of defining the new layouts buttons, textview etc in my main class , I want to have a separate class which can initialize my buttons etc, and also in that class I can declare onClickListners.
In my main Activity I want:-
public void onCreate(){
button bb = (Button)findViewById(R.id.mybutton);
View CurrentContentView= getLayoutInflater().inflate(R.layout.activity_main, null, false);
bb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new MyNewViewInit(CurrentContentView);
}
});
}
In my MyNewViewInit() class :-
public class MyNewViewInit{
ImageButton backbutton;
ChipsMultiAutoCompleteTextview search;
ImageButton searchclear;
ImageButton modeTime;
ImageButton modeTag;
TextView modeTimeTxt;
TextView modeTagTxt;
ScrollView mainScroll;
ScrollView selectedScroll;
public MyNewViewInit(View v){
backbutton = (ImageButton)v.findViewById(R.id.backbutton);
search = (ChipsMultiAutoCompleteTextview)v.findViewById(R.id.search);
searchclear = (ImageButton)v.findViewById(R.id.searchclear);
modeTime = (ImageButton)v.findViewById(R.id.modeTime);
modeTag = (ImageButton)v.findViewById(R.id.modeTag);
modeTimeTxt = (TextView)v.findViewById(R.id.modeTimeTxt);
modeTagTxt = (TextView)v.findViewById(R.id.modeTagTxt);
mainScroll = (ScrollView)v.findViewById(R.id.HAT1);
selectedScroll = (ScrollView)v.findViewById(R.id.HAT2);
tag = new OtherHelper().arrayread();
mainHashTag.setVisibility(View.GONE);
selectedHashTag.setVisibility(View.GONE);
clickListners();
}
public void clickListners(){
backbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
searchclear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
modeTime.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
modeTimeTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
modeTag.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
modeTagTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
}
So when I try using this code the onclicklistners are not working.
What is the best way to do this.
Thanks
Write an Interface in your class,
class MyNewViewInit{
public interface ClickListenerInterface{
void onCLickSomething(Something something);
}
ClickListenerInterface mClickListener;
...
public MyNewViewInit(View v,ClickListenerInterface clickListener){
mClickListener = clickListener;
...
}
public void clickListners(){
...
backbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mClickListener!=null){
mClickListener.onCLickSomething(v);
}else{
//throw an error that activity should implement ClickListenerInterface
}
}
});
}
}
In your Activity,
class MyActivity extends Activity implements ClickListenerInterface{
public void onCreate(){
bb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new MyNewViewInit(CurrentContentView,this);
//if using Android Studio, then use the context menu to implement the interface
}
});
}
void onCLickSomething(Something something){
//do something with something
}
}
You can try do a abstract class extends Activity, like :
public abstract class MyInitActivity extends Activity
{
EditText editText;
TextView textView;
#Override
public void setContentView(int layoutResID)
{
super.setContentView(layoutResID);
editText = (EditText)findViewById(R.id.editText);
textView = (TextView)findViewById(R.id.textView);
}
}
and in your main activity you can extend your abstract class, like :
public class MainActivity extends MyInitActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/*********** Listener ************/
public void onButtonClick(View v)
{
textView.setText(editText.getText().toString());
}
}
you can also implement onClick() in the abstract class
i don't know if its a good practice , i prefer do the methods with view arguments and set this methods in the layout.xml file .
I had use this in one of code
STEP 1
Declare button in layout(XML file)
STEP 2
Declare all ur button like this--
Button btn1 = (Button)findViewById(R.id.btn1);
Button btn2 = (Button)findViewById(R.id.btn2);
Button btn3 = (Button)findViewById(R.id.btn3);
............so on
STEP 3
implement View.OnClickListener
Now after step 2 do this
btn1.setOnClickListener(this);
.
.
.
same way for all
STEP 4:
Use switch case inside Onclick
public void OnClick(View v)
{
case:R.id.btn1{
//ur command similarly use multiple case stmt for many button
}
}

how solve this error masage related str "this variable must be final to be used in local class"

public class saeidactivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.saeid);
Button btn=(Button) findViewById(R.id.saeidbtn1);
TextView str=(TextView) findViewById(R.id.saeidtxtv1);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
str.setTextColor(0xFF00FF00);
}
});
}
}
Try to add final modifier.
final TextView str=(TextView)
instead of
TextView str=(TextView)
Or, you can make the TextView local.
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
TextView str=(TextView) findViewById(R.id.saeidtxtv1);
if(str != null)
str.setTextColor(0xFF00FF00);
}
});

Caused by: java.lang.NullPointerException in android studio

i have 6 layouts and each layout having two button back and next..without button back and next application runs properly but when i put code in back and next button having name button 7 is used for back and button 8 is used for next layout it gives me a error null exception.
ImageButton btn1;
ImageButton btn2;
ImageButton btn3;
ImageButton btn4;
ImageButton btn5;
ImageButton btn6;
ImageButton btn7;
ImageButton btn8;
ImageView view;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton7();
//addListenerOnButton8();
addListenerOnButton();
addListenerOnButton2();
addListenerOnButton3();
addListenerOnButton4();
addListenerOnButton5();
addListenerOnButton6();
public void addListenerOnButton7() {
btn7= (ImageButton) findViewById(R.id.btn7);
btn7.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.show);
Runtime.getRuntime().gc();
}
}
);
}
public void addListenerOnButton() {
btn1= (ImageButton) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.show);
Runtime.getRuntime().gc();
}
}
);
}
public void addListenerOnButton2() {
btn2= (ImageButton) findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k2);
Runtime.getRuntime().gc();
}
}
);
}
public void addListenerOnButton3() {
btn3= (ImageButton) findViewById(R.id.btn3);
btn3.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k3);
Runtime.getRuntime().gc();
}
}
);
}
public void addListenerOnButton4() {
btn4= (ImageButton) findViewById(R.id.btn4);
btn4.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k4);
Runtime.getRuntime().gc();
}
}
);
}
public void addListenerOnButton5() {
btn5= (ImageButton) findViewById(R.id.btn5);
btn5.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k5);
Runtime.getRuntime().gc();
}
}
);
}
public void addListenerOnButton6() {
btn6= (ImageButton) findViewById(R.id.btn6);
btn6.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k6);
Runtime.getRuntime().gc();
}
}
);
}
You already use it in there
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Looking at the code, it looks like it comes from inside an activity so you need to call setContentView(R.layout.activity_main); in the onCreate() of your activity before btn7= (ImageButton) findViewById(R.id.btn7); and that should fix the NullPointer.

Categories

Resources