For some reason this came up when trying to switch activities. I've had no problem running the same kind of code with other activities in this project. I'll post the code and see if anyone can figure it out ;(
I can't get my error to format right in this, so here's the error log
http://textuploader.com/oe6g
Here's my different activities.
The Problem occurs when I click on the button in my New Game Activity Layout, and that button runs the confirm(View view)
NewGameActivity.java
public class NewGameActivity extends Activity {
EditText title;
TextView team1, team2;
SqliteHelper db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_newgame);
//ListViews
team1 = (TextView) findViewById(R.id.textView4);
team2 = (TextView) findViewById(R.id.textView5);
title = (EditText) findViewById(R.id.editText1);
db = new SqliteHelper(this.getApplicationContext());
populateListViews();
registerClickCallback();
}
/*
*
* Database Functions
*
*/
/*
*
* ListView Stuff
*
*/
private void populateListViews() {
// THIS HERE WILL POPULATE BOTH TEAM LIST VIEWS
//Create list of items
Cursor cursor = db.getTeams();
ArrayList<String> values = new ArrayList<String>();
if (cursor != null && cursor.getCount() != 0) {
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
values.add(cursor.getString(cursor.getColumnIndex("Team_Names")));
cursor.moveToNext();
}
}
//Build Adapter
ArrayAdapter<String> t1adapter = new ArrayAdapter<String>(
this, // Context
R.layout.teamlistviews, // Layout to use
values); // Items to be displayed
ArrayAdapter<String> t2adapter = new ArrayAdapter<String>(
this, // Context
R.layout.teamlistviews, // Layout to use
values);
//Configure the List View
ListView t1list = (ListView) findViewById(R.id.listView1);
t1list.setAdapter(t1adapter);
ListView t2list = (ListView) findViewById(R.id.listView2);
t2list.setAdapter(t2adapter);
}
private void registerClickCallback() {
//This uses the List View and adds a listener to check for clicks/taps on different
//list view items. It will then display a message telling you which one you have selected.
ListView t1list = (ListView) findViewById(R.id.listView1);
ListView t2list = (ListView) findViewById(R.id.listView2);
t1list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View viewClicked,
int position, long id) {
//team 1
TextView textView = (TextView) viewClicked;
//Changing team name
TextView t1 = (TextView) findViewById(R.id.textView4);
String team1 = textView.getText().toString();
t1.setText(team1);
//Toast message
String message = "You Selected " + textView.getText().toString() + " for Team 1";
Toast.makeText(NewGameActivity.this, message, Toast.LENGTH_SHORT).show();
}
});
t2list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View viewClicked,
int position, long id) {
//team 2
TextView textView = (TextView) viewClicked;
//Changing Team name
TextView t2 = (TextView) findViewById(R.id.textView5);
String team2 = textView.getText().toString();
t2.setText(team2);
//Toast message
String message = "You Selected " + textView.getText().toString() + " for Team 2";
Toast.makeText(NewGameActivity.this, message, Toast.LENGTH_SHORT).show();
}
});
}
public void confirm(View view) {
String t1, t2, gTitle;
t1 = team1.toString();
t2 = team2.toString();
gTitle = title.toString();
Intent intent = new Intent(this, CourtActivity.class);
intent.putExtra("TEAM1", t1);
intent.putExtra("TEAM2", t2);
intent.putExtra("GAME_TITLE", gTitle);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.new_game, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
CourtActivity.java
package com.example.statapalpha;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.Math;
import java.util.ArrayList;
// Court Screen
public class CourtActivity extends Activity implements OnMenuItemClickListener{
// Opens database
SqliteHelper db;
ArrayList<String> homePlayersIn = new ArrayList<String>();
ArrayList<String> awayPlayersIn = new ArrayList<String>();
ArrayList<String> homePlayersBench = new ArrayList<String>();
ArrayList<String> awayPlayersBench = new ArrayList<String>();
String team1, team2, team1n, team2n;
String player = "0"; // Player number for current play
String action = ""; // Action text for current play
position position = new position(); // Position for current play
int playNumber = 0;
private PopupMenu popupMenu;
boolean isHome = false;
int playerButton = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_court);
db = new SqliteHelper(this.getApplicationContext());
//Get Team 1 and 2 and Game Title
convertStrings();
// Gets players
getPlayers();
}
public void convertStrings() {
Intent mIntent = getIntent();
team1n = mIntent.getStringExtra("TEAM1");
team2n = mIntent.getStringExtra("TEAM2");
}
// Populates arrays with player numbers
void getPlayers() {
homePlayersBench.add("12");
homePlayersBench.add("10");
homePlayersBench.add("32");
homePlayersBench.add("16");
homePlayersBench.add("13");
homePlayersBench.add("19");
homePlayersIn.add("13");
homePlayersIn.add("14");
homePlayersIn.add("15");
homePlayersIn.add("16");
homePlayersIn.add("17");
awayPlayersBench.add("21");
awayPlayersBench.add("22");
awayPlayersBench.add("23");
awayPlayersBench.add("24");
awayPlayersBench.add("25");
awayPlayersBench.add("26");
awayPlayersIn.add("31");
awayPlayersIn.add("32");
awayPlayersIn.add("33");
awayPlayersIn.add("34");
awayPlayersIn.add("35");
}
// Stores x and y coordinate
public class position {
public int x;
public int y;
}
// Sets current player when a player button is clicked
public void setPlayer(View v) {
Button b = (Button)v;
player = b.getText().toString();
playerButton = b.getId();
switch(b.getId()) {
case R.id.p1:
case R.id.p2:
case R.id.p3:
case R.id.p4:
case R.id.p5: isHome = true; break;
default: isHome = false;
break;
}
}
// Sets string Action to whatever action the user taps
// then records play to database.
public void setAction(View v) {
Button b = (Button)v;
String toastAction = "";
String message = "";
String team = "";
switch(b.getId()) {
case R.id.fgMade: action = "F" + goal(position) + "H"; toastAction = "made " + goal(position) + " point shot";
break;
case R.id.fgMissed: action = "F" + goal(position) + "M"; toastAction = "missed " + goal(position) + " point shot";
break;
case R.id.ftMade: action = "FTH"; toastAction = "made freethrow";
break;
case R.id.ftMissed: action = "FTM"; toastAction = "missed freethrow";
break;
case R.id.rebound: action = "RB"; toastAction = "rebound";
break;
case R.id.assist: action = "AST"; toastAction = "assist";
break;
case R.id.block: action = "BL"; toastAction = "block";
break;
case R.id.steal: action = "STL"; toastAction = "steal";
break;
case R.id.turnover: action = "TO"; toastAction = "turnover";
break;
case R.id.sub: action = "SUB"; toastAction = "substitution";
break;
case R.id.foul: action = "FC"; toastAction = "commited foul";
break;
case R.id.undoPlay: undoPlay(v);
break;
}
if (action == "SUB") {
popupMenu = new PopupMenu(this.getBaseContext(), v);
Menu menu = popupMenu.getMenu();
if (isHome == true) {
for (String number : homePlayersBench) {
menu.add(number);
}
}
else {
for (String number : awayPlayersBench) {
menu.add(number);
}
}
popupMenu.setOnMenuItemClickListener(this);
popupMenu.show();
return;
}
else {
message = "Player " + player + " " + toastAction + " at (" + Integer.toString(position.x) + ", " + Integer.toString(position.y) + ")";
}
if (isHome == true)
team = team1;
else
team = team2;
playNumber++;
db.recordPlay(Integer.parseInt(player), team, action, position, playNumber);
Toast.makeText(CourtActivity.this, message, Toast.LENGTH_SHORT).show();
refreshPlayers();
}
// Gets tap position and saves it to 'position'
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
position.x = (int)event.getX(0);
position.y = (int)event.getY(0);
}
return super.onTouchEvent(event);
}
int goal(position position) {
int points=3;
if ((Math.sqrt(Math.pow((position.x - 1082), 2) + Math.pow((position.y - 453), 2)) < 270) || (Math.sqrt(Math.pow((position.x - 193), 2) + Math.pow((position.y - 453), 2)) < 270)) points = 2;
return points;
}
public void undoPlay(View v) {
db.undoPlay(Integer.toString(playNumber));
}
#Override
public boolean onMenuItemClick(MenuItem item) {
Button button = (Button)findViewById(playerButton);
button.setText(item.getTitle());
refreshPlayers();
return false;
}
}
activity_court.xml
<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:background="#drawable/bg2court"
tools:context="${relativePackage}.${activityClass}" xmlns:android1="http://schemas.android.com/apk/res/android">
<Button
android:id="#+id/editPlays"
android:layout_width="190dp"
android:layout_height="75dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="editPlays"
android:text="#string/edit_plays"
/>
<Button
android:id="#+id/undoPlay"
android:layout_width="190dp"
android:layout_height="75dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/editPlays"
android:onClick="undoPlay"
android:text="#string/undo" />
<Button
android:id="#+id/ftMissed"
android:layout_width="190dp"
android:layout_height="85dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="setAction"
android:text="#string/miss" />
<Button
android:id="#+id/ftMade"
android:layout_width="190dp"
android:layout_height="85dp"
android:layout_above="#+id/ftMissed"
android:layout_alignParentRight="true"
android:onClick="setAction"
android:text="#string/made" />
<Button
android:id="#+id/fgMade"
android:layout_width="190dp"
android:layout_height="85dp"
android:layout_above="#+id/fgMissed"
android:layout_alignParentRight="true"
android:onClick="setAction"
android:text="#string/made" />
<Button
android:id="#+id/fgMissed"
android:layout_width="190dp"
android:layout_height="85dp"
android:layout_above="#+id/ftMade"
android:layout_alignParentRight="true"
android:layout_marginBottom="52dp"
android:onClick="setAction"
android:text="#string/miss" />
<TextView
android:id="#+id/fgText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/fgMade"
android:layout_alignLeft="#+id/playerStats"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="#string/field_goal"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="38sp" />
<Button
android:id="#+id/playerStats"
android:layout_width="190dp"
android:layout_height="75dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/undoPlay"
android:onClick="redoPlay"
android:text="#string/playerStats" />
<TextView
android:id="#+id/ftText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/ftMade"
android:layout_alignRight="#+id/fgText"
android1:layout_alignLeft="#+id/ftMade"
android:text="#string/free_throw"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="34sp" />
<Button
android:id="#+id/foul"
android:layout_width="190dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ftMade"
android:onClick="setAction"
android:color= "#FF0079FF"
android:text="#string/foul" />
<Button
android:id="#+id/steal"
android:layout_width="190dp"
android:layout_height="80dp"
android:layout_above="#+id/turnover"
android:layout_alignParentLeft="true"
android:onClick="setAction"
android:text="#string/steal" />
<Button
android:id="#+id/block"
android:layout_width="190dp"
android:layout_height="80dp"
android:layout_above="#+id/steal"
android:layout_alignParentLeft="true"
android:onClick="setAction"
android:text="#string/block" />
<Button
android:id="#+id/assist"
android:layout_width="190dp"
android:layout_height="80dp"
android:layout_above="#+id/block"
android:layout_alignParentLeft="true"
android:onClick="setAction"
android:text="#string/assist" />
<Button
android:id="#+id/rebound"
android:layout_width="190dp"
android:layout_height="80dp"
android:layout_above="#+id/assist"
android:layout_alignParentLeft="true"
android:onClick="setAction"
android:text="#string/rebound" />
<Button
android:id="#+id/p1"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/rebound"
android:onClick="setPlayer"
android:text="#string/p1" />
<Button
android:id="#+id/p2"
android:layout_width="85dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/p1"
android:onClick="setPlayer"
android:text="#string/p2" />
<Button
android:id="#+id/p3"
android:layout_width="85dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/p2"
android:onClick="setPlayer"
android:text="#string/p3" />
<Button
android:id="#+id/p4"
android:layout_width="85dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/p3"
android:onClick="setPlayer"
android:text="#string/p4" />
<Button
android:id="#+id/p5"
android:layout_width="85dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/p4"
android:onClick="setPlayer"
android:text="#string/p5" />
<Button
android:id="#+id/p10"
android:layout_width="85dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/editPlays"
android:onClick="setPlayer"
android:text="#string/p10" />
<Button
android:id="#+id/p9"
android:layout_width="85dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/p10"
android:onClick="setPlayer"
android:text="#string/p9" />
<Button
android:id="#+id/p8"
android:layout_width="85dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/p9"
android:onClick="setPlayer"
android:text="#string/p8" />
<Button
android:id="#+id/p7"
android:layout_width="85dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/p8"
android:onClick="setPlayer"
android:text="#string/p7" />
<Button
android:id="#+id/p6"
android:layout_width="85dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/p7"
android:onClick="setPlayer"
android:text="#string/p6" />
<Button
android:id="#+id/sub"
android:layout_width="190dp"
android:layout_height="80dp"
android:layout_above="#+id/foul"
android:layout_toLeftOf="#+id/p1"
android:onClick="setAction"
android:text="#string/sub" />
<Button
android:id="#+id/turnover"
android:layout_width="190dp"
android:layout_height="80dp"
android:layout_above="#+id/sub"
android:layout_toLeftOf="#+id/p1"
android:onClick="setAction"
android:text="#string/turnover" />
<TextView
android:id="#+id/points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/court"
android:layout_alignRight="#+id/rebound"
android:text="#string/points"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/fouls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/undoPlay"
android:layout_toLeftOf="#+id/p1"
android:text="#string/fouls"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p1p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/points"
android:layout_alignRight="#+id/p1"
android:layout_marginRight="35dp"
android:text="#string/p1p"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p1f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/fouls"
android:layout_alignRight="#+id/p1"
android:layout_marginRight="35dp"
android:text="#string/p1f"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p2p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/points"
android:layout_alignRight="#+id/p2"
android:layout_marginRight="35dp"
android:text="#string/p2p"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p2f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/fouls"
android:layout_alignRight="#+id/p2"
android:layout_marginRight="35dp"
android:text="#string/p2f"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p3p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/points"
android:layout_alignRight="#+id/p3"
android:layout_marginRight="35dp"
android:text="#string/p3p"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p3f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/fouls"
android:layout_alignRight="#+id/p3"
android:layout_marginRight="35dp"
android:text="#string/p3f"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p4p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/points"
android:layout_alignRight="#+id/p4"
android:layout_marginRight="35dp"
android:text="#string/p4p"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p4f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/fouls"
android:layout_alignRight="#+id/p4"
android:layout_marginRight="35dp"
android:text="#string/p4f"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p5p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/points"
android:layout_alignRight="#+id/p5"
android:layout_marginRight="35dp"
android:text="#string/p5p"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p5f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/fouls"
android:layout_alignRight="#+id/p5"
android:layout_marginRight="35dp"
android:text="#string/p5f"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p6p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/points"
android:layout_alignRight="#+id/p6"
android:layout_marginRight="35dp"
android:text="#string/p6p"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p6f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/fouls"
android:layout_alignRight="#+id/p6"
android:layout_marginRight="35dp"
android:text="#string/p6f"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p7p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/points"
android:layout_alignRight="#+id/p7"
android:layout_marginRight="35dp"
android:text="#string/p7p"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p7f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/fouls"
android:layout_alignRight="#+id/p7"
android:layout_marginRight="35dp"
android:text="#string/p7f"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p8p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/points"
android:layout_alignRight="#+id/p8"
android:layout_marginRight="35dp"
android:text="#string/p8p"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p8f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/fouls"
android:layout_alignRight="#+id/p8"
android:layout_marginRight="35dp"
android:text="#string/p8f"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p9p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/points"
android:layout_alignRight="#+id/p9"
android:layout_marginRight="35dp"
android:text="#string/p9p"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p9f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/fouls"
android:layout_alignRight="#+id/p9"
android:layout_marginRight="35dp"
android:text="#string/p9f"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p10p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/points"
android:layout_alignRight="#+id/p10"
android:layout_marginRight="35dp"
android:text="#string/p10p"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p10f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/fouls"
android:layout_alignRight="#+id/p10"
android:layout_marginRight="35dp"
android:text="#string/p10f"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/court"
android:layout_width="900dp"
android:layout_height="600dp"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/p10"
android:contentDescription="#string/undo"
android:src="#drawable/court" />
</RelativeLayout>
I mean it tells me the error occurs in the onCreate of the CourtActivity, however I can't see anything that's wrong. This is part of my school project, I'm only concerned with the things that are causing me errors currently, not too worried about anything else.
Note:This is a group project. The courtactivity screen is the only one I've not had a part in making, so I'm not sure exactly what went into it. However now I'm trying to put everything together and running into this.
Anything helps! Thanks!
The error is happening while attempting to load an image specified on the Layout file, as reported by the following part of the log:
...: Caused by: java.lang.OutOfMemoryError
...: E/AndroidRuntime(2137): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
...: E/AndroidRuntime(2137): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:503)
I bet if you remove the line android:background="#drawable/bg2court" from the root container (RelativeLayout) in the file activity_court.xml the error will be gone.
If that is the case I would suggest you to try a smaller (lower resolution) version of that image.
While looking for a replacement, keep an eye on the heap memory allocation messages on logcat so you will know how much progress you are making.
Loading high resolution images on Android is usually a tricky process because of the following reasons:
Bitmap files loaded in memory have to be decompressed so they can be rendered, making them a lot larger then their original .JPG and .PNG files. To calculate how many bytes of memory will be allocated to load a given image, just follow this simple formula: width (pixels) x height (pixels) x number bytes per pixel (3 for 24 bit images). Example: a bitmap with the size of 2048x2048 pixels takes exactly 12mb of memory;
Bitmaps are loaded into the heap space which happens to be a pretty small portion of memory given by the OS to your app when it starts. The size of the heap space usually ranges from 16 to 64mb depending on the device.
Finally, I would suggest one of the following approaches:
Replace the large background image by a lower resolution version that can be scaled up to fill the background space (at the cost of quality of corse);
Load bitmaps programmatically. There are BitmapFactory methods that can downsample images on the fly, during the load process. More on this here;
Use a 3rd party library to do it for you. Picasso is an excelent choice.
The error is coming from inflating the layout. It's an OutOfMemoryError. Do you have some really huge bitmap or more likely a circular dependency? Can you upload the xml file R.layout.activity_court?
I had this error no long ago but got some help from the guys in here
here is what I did
if your background / image don't need transpart stuff or like keep in JPG format as it will be smaller. this should sort your Out of Memory issue temp. but when you have more image coming up you will still have problem (Out of Memory) coming up again
hope this can help you for the time been
Related
Why when I click the button and nothing is written in EditText, the program crashes?
The app is used to calculate the load securing.Users enter values and get the number of straps they need.But when a field is free, the app crashes
Code:
public class Ladungssicherung extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
EditText stfinput;
EditText gewichtinput;
EditText winkelinput;
String text;
double k = 1.5;
int cZ = 1; // beschleunigungsbeiwert nach unten
double cX = 0.8; // beschleunigungsbeiwert nach vorne
Dialog epicDialog;
TextView unicode, ergebnissFeld;
ImageView muinfoButton, closemuButton, infoalphaButton;
#SuppressLint("StringFormatInvalid")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_ladungssicherung);
winkelinput = (EditText) findViewById(R.id.winkelInput);
ergebnissFeld = (TextView) findViewById(R.id.ergebniss);
gewichtinput = (EditText) findViewById(R.id.gewichtInput);
stfinput = (EditText) findViewById(R.id.stfInput);
SharedPreferences sharedPreferences = getSharedPreferences("sharedPrefs", MODE_PRIVATE);
text = sharedPreferences.getString("text", "");
winkelinput.setText(text);
sharedPreferences.edit().remove("text").commit();
epicDialog = new Dialog(this, android.R.style.Theme_DeviceDefault_Light_NoActionBar_Fullscreen);
muinfoButton = (ImageView) findViewById(R.id.infomuIcon);
closemuButton = (ImageView) findViewById(R.id.closemuinfo);
infoalphaButton = (ImageView) findViewById(R.id.infoalpha);
muinfoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showmuinfo();
}
});
infoalphaButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showalphawinkel();
}
});
//Spinner code
Spinner spinner = findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.numbers, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
public void showalphawinkel() {
Intent intent = new Intent(this, WinkelmessActivity.class);
startActivity(intent);
}
public void showmuinfo() {
epicDialog.setContentView(R.layout.muinfo);
closemuButton = (ImageView) epicDialog.findViewById(R.id.closemuinfo);
closemuButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
epicDialog.dismiss();
}
});
epicDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
epicDialog.show();
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
public void ergebnissFromel(View v) {
Spinner feld4 = (Spinner) findViewById(R.id.spinner1);
Integer zahl1 = Integer.parseInt(gewichtinput.getText().toString());
Integer zahl2 = Integer.parseInt(winkelinput.getText().toString());
Integer zahl3 = Integer.parseInt(stfinput.getText().toString());
String spinner = feld4.getSelectedItem().toString();
xml layout:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Ladungssicherung">
<ImageView
android:id="#+id/infoalpha"
android:layout_width="38dp"
android:layout_height="40dp"
android:layout_alignBottom="#+id/winkelInput"
android:layout_alignStart="#+id/infomuIcon"
app:srcCompat="#drawable/info_icon" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="97dp"
android:layout_height="34dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="223dp"
android:layout_toEndOf="#+id/textView">
</Spinner>
<TextView
android:id="#+id/textView"
android:layout_width="117dp"
android:layout_height="29dp"
android:layout_above="#+id/gewichtInput"
android:layout_alignStart="#+id/gewichtInput"
android:layout_marginBottom="-108dp"
android:text="Gewicht"
android:textSize="20sp"
android:textStyle="bold|italic" />
<EditText
android:id="#+id/gewichtInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="108dp"
android:ems="10"
android:hint="kg"
android:inputType="textPersonName" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/winkelInput"
android:layout_marginBottom="-211dp"
android:layout_toStartOf="#+id/spinner1"
android:text="Winkel Alpha"
android:textSize="20sp"
android:textStyle="bold|italic" />
<EditText
android:id="#+id/winkelInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="211dp"
android:ems="10"
android:hint="#string/alpha"
android:inputType="textPersonName" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/stfInput"
android:layout_alignStart="#+id/textView"
android:text="Vorspannkraft je Gurt"
android:textSize="20sp"
android:textStyle="bold|italic" />
<EditText
android:id="#+id/stfInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView5"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="daN"
android:inputType="textPersonName" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="37dp"
android:layout_height="29dp"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/textView5"
android:layout_marginTop="116dp"
app:srcCompat="#drawable/gewicht" />
<View
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="175dp"
android:background="#color/grau">
</View>
<View
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="277dp"
android:background="#color/grau">
</View>
<View
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="275dp"
android:background="#color/grau">
</View>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/spinner1"
android:layout_marginStart="46dp"
android:text="#string/mue"
android:textSize="25sp" />
<TextView
android:id="#+id/textView5"
android:layout_width="15dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="-3dp"
android:layout_toStartOf="#+id/textView6"
android:text="S"
android:textSize="23sp" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/stfInput"
android:layout_marginStart="3dp"
android:layout_alignStart="#+id/textView4"
android:text="TF"
android:textSize="23sp" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/winkelInput"
android:layout_alignStart="#+id/textView4"
android:text="#string/alpha"
android:textSize="23sp" />
<ImageView
android:id="#+id/infomuIcon"
android:layout_width="38dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/spinner1"
android:layout_marginTop="-2dp"
android:layout_marginEnd="17dp"
app:srcCompat="#drawable/info_icon" />
<TextView
android:id="#+id/titelNiederzurren"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:textSize="20sp"
android:textColor="#color/blue"
android:text="Niederzurren" />
<View
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="199dp"
android:background="#color/grau">
</View>
<TextView
android:id="#+id/ergebniss"
android:layout_width="154dp"
android:layout_height="36dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="53dp"
android:background="#color/greensmiley"
android:text="" />
<Button
android:id="#+id/buttonladungssicherung"
android:layout_width="144dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="120dp"
android:text="Button"
android:onClick="ergebnissFromel"
/>
</RelativeLayout>
You are probably getting NumberFormatException because you are trying to parse empty String to Integer. Surround Integer.parseInt(gewichtinput.getText().toString()) lines with try catch blocks, and it should be fine.
Try these codes and let us get updated with what you see:
public void ergebnissFromel(View v) {
Spinner feld4 = (Spinner) findViewById(R.id.spinner1);
Integer zahl1 = 0;
Integer zahl2 = 0;
Integer zahl3 = 0;
try
{
zahl1 = Integer.parseInt(gewichtinput.getText().toString());
zahl2 = Integer.parseInt(winkelinput.getText().toString());
zahl3 = Integer.parseInt(stfinput.getText().toString());
} catch (Exception e) {
Toast.makeText(this,e.getMessage(), Toast.LENGTH_SHORT).show();
return;
}
String spinner = feld4.getSelectedItem().toString();
if ( zahl1.equals("") || zahl1 == null){
Toast.makeText(this, "Bitte geben Sie das Gewicht an", Toast.LENGTH_SHORT).show();
}
if ( zahl2.equals("") || zahl2 == null){
Toast.makeText(this, "Bitte geben Sie das Gewicht an", Toast.LENGTH_SHORT).show();
}
if ( zahl3.equals("") || zahl3 == null){
Toast.makeText(this, "Bitte geben Sie das Gewicht an", Toast.LENGTH_SHORT).show();
}
else {
// Grad wird in sinus alpha umgerechnet
double newsinus = Math.sin(Math.toRadians(Float.valueOf(zahl2)));
double wert1 = cX - Float.valueOf(spinner) * cZ;
double wert2 = Float.valueOf(spinner) * newsinus;
// Formel Niederzurren Algorithmus
double wert3 = wert1 * zahl1;
double wert4 = wert2 * k;
double wert5 = wert3 / wert4;
double wert6 = Math.round(wert5);
double wert7 = wert6 / zahl3;
double wert8 = Math.ceil(wert7);
ergebnissFeld.setText(String.valueOf(Math.round(wert8)));
}
}
Usually when your app crashes, you'll find a stack trace logged that will help you understand where the problem is. You can find the log within Android Studio's Debug|Console tab. Alternatively you can use the adb command from a terminal/shell window. I like using "adb logcat -v time".
Before parsing to integer, you should check wether the input is null.
Furthermore, you're calling a method on zahl1 and check wether it is null afterwards.
String strZahl1 = gewichtinput.getText().toString(); //Get input as String
if(strZahl1 == null || strZahl1.isEmpty()) //First make sure that strZahl1 is not null, then check wether it is empty
{
Toast.makeText(this, "Bitte geben Sie das Gewicht an",Toast.LENGTH_SHORT).show();
}
Integer zahl1 = Integer.parseInt(strZahl1);
If the user is able to enter something else than a number into your textfields, you could surround the parsing with a try block:
Integer zahl1;
try
{
zahl1 = Integer.parseInt(strZahl1);
}
catch(NumberFormatException e)
{
Toast.makeText(this, strZahl1 + " ist keine gültige Zahl", Toast.LENGTH_SHORT).show();
}
Your app crashes because .getText().toString is an empty string. Java cannot convert an empty string into an integer.
A better way is to do something like
Integer zahl1 ;
if((gewichtinput.getText() != null) && (!gewichtinput.getText().toString.isEmpty()))
{
zahl1 = Integer.parseInt(gewichtinput.getText().toString());
}
else
{
zahl1 = 0 ;
}
Do the same for all the other edit texts.
It should hopefully work
I am new to app development, I am stuck on this following example in my book.
Can some one please help me that why my play button is not working? After clicking on the play button the game should start and it should navigate to next activity.
Main page 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"
tools:context="com.packtpub.mathgamechapter3a.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="My Math Game"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:textSize="30sp" />
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/imageView"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:id="#+id/buttonPlay"
android:layout_marginTop="28dp"
android:layout_below="#+id/imageView"
android:layout_alignRight="#+id/button2"
android:layout_alignEnd="#+id/button2"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="High Scores"
android:id="#+id/button2"
android:layout_below="#+id/buttonPlay"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quit"
android:id="#+id/button3"
android:layout_below="#+id/button2"
android:layout_alignRight="#+id/buttonPlay"
android:layout_alignEnd="#+id/buttonPlay"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2" />
</RelativeLayout>
Java code
package com.packtpub.mathgamechapter3a;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button buttonPlay = (Button)findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Intent i;
i = new Intent(this, GameActivity.class);
startActivity(i);
}
}
Game page where it should navigate
<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"
tools:context="com.packtpub.mathgamechapter3a.GameActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="2"
android:id="#+id/textPartA"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="24dp"
android:textSize="70sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="x"
android:id="#+id/textOperator"
android:layout_alignTop="#+id/textPartA"
android:layout_centerHorizontal="true"
android:textSize="70sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="2"
android:id="#+id/textPartB"
android:layout_alignTop="#+id/textOperator"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="70sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="="
android:id="#+id/textView4"
android:layout_below="#+id/textOperator"
android:layout_centerHorizontal="true"
android:layout_marginTop="92dp"
android:textSize="70sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="#+id/buttonChoice1"
android:layout_below="#+id/textView4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="99dp"
android:textSize="40sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="#+id/buttonChoice2"
android:layout_alignTop="#+id/buttonChoice1"
android:layout_centerHorizontal="true"
android:textSize="40sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:id="#+id/buttonChoice3"
android:layout_alignTop="#+id/buttonChoice2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="40sp" />
</RelativeLayout>
Java code
package com.packtpub.mathgamechapter3a;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class GameActivity extends Activity implements View.OnClickListener{
int correctAnswer;
Button buttonObjectChoice1;
Button buttonObjectChoice2;
Button buttonObjectChoice3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//The next line loads our UI design to the screen
setContentView(R.layout.activity_game);
//Here we initialize all our variables
int partA = 9;
int partB = 9;
correctAnswer = partA * partB;
int wrongAnswer1 = correctAnswer - 1;
int wrongAnswer2 = correctAnswer + 1;
/*Here we get a working object based on either the button
or TextView class and base as well as link our new objects
directly to the appropriate UI elements that we created previously*/
TextView textObjectPartA = (TextView)findViewById(R.id.textPartA);
TextView textObjectPartB = (TextView)findViewById(R.id.textPartB);
buttonObjectChoice1 = (Button)findViewById(R.id.buttonChoice1);
buttonObjectChoice2 = (Button)findViewById(R.id.buttonChoice2);
buttonObjectChoice3 = (Button)findViewById(R.id.buttonChoice3);
//Now we use the setText method of the class on our objects
//to show our variable values on the UI elements.
textObjectPartA.setText("" + partA);
textObjectPartB.setText("" + partA);
//which button receives which answer, at this stage is arbitrary.
buttonObjectChoice1.setText("" + correctAnswer);
buttonObjectChoice2.setText("" + wrongAnswer1);
buttonObjectChoice3.setText("" + wrongAnswer2);
buttonObjectChoice1.setOnClickListener(this);
buttonObjectChoice2.setOnClickListener(this);
buttonObjectChoice3.setOnClickListener(this);
}//onCreate ends here
#Override
public void onClick(View view) {
//declare a new int to be used in all the cases
int answerGiven=0;
switch (view.getId()) {
case R.id.buttonChoice1:
//initialize a new int with the value contained in buttonObjectChoice1
//Remember we put it there ourselves previously
answerGiven = Integer.parseInt("" + buttonObjectChoice1.getText());
//is it the right answer?
if(answerGiven==correctAnswer) {//yay it's the right answer
Toast.makeText(getApplicationContext(),
"Well done!", Toast.LENGTH_LONG).show();
}else{//uh oh!
Toast.makeText(getApplicationContext(),
"Sorry that's wrong", Toast.LENGTH_LONG).show();
}
break;
case R.id.buttonChoice2:
//same as previous case but using the next button
answerGiven = Integer.parseInt("" + buttonObjectChoice2.getText());
if(answerGiven==correctAnswer) {
Toast.makeText(getApplicationContext(), "Well done!",
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),
"Sorry that's wrong", Toast.LENGTH_LONG).show();
}
break;
case R.id.buttonChoice3:
//same as previous case but using the next button
answerGiven = Integer.parseInt("" + buttonObjectChoice3.getText());
if(answerGiven==correctAnswer) {
Toast.makeText(getApplicationContext(), "Well done!",
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),"Sorry that's wrong",
Toast.LENGTH_LONG).show();
}
break;
}
}
}
i checked your code it is correct .
I think you forget to register your activity in manifests
please check that
well you need to do a if statement on the being clicked on.
if(view.getId() == R.id.buttonPlay)
{
Intent intent = new Intent(getBaseContext(), GameActivity.class);
startActivity(intent);
}
also make sure game activity is registered in the manifest file.
Intent in = getIntent();
is missing..
the 2nd activity should contain this line
also add this
if(view.getId() == R.id.buttonPlay)
{
Intent inte = new Intent(MainActivity.this, GameActivity.class);
startActivity(intent);
}
and do register in the manifest file
The app executed just fine, and the interface loads just fine as well, but when I try to press any of the buttons, nothing that should happen happens. The point is fairly simple: press the '+' button, the quantity increases, and the price immediately updates to match said quantity, vice versa for the '-' button. I don't know what I've done wrong, but any button interaction with the app crashes it.
Here's my Java:
package com.t99sdevelopment.mobile.eyy;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.text.NumberFormat;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public int quantityValue = 1;
public void increaseQuantity (View view) {
quantityValue = quantityValue + 1;
updateQuantityValue(view);
}
public void decreaseQuantity (View view) {
quantityValue = quantityValue - 1;
updateQuantityValue(view);
}
public void updateQuantityValue(View view) {
updateQuantity(quantityValue);
updatePrice(quantityValue);
}
private void updateQuantity(int number) {
TextView quantity = (TextView) findViewById(
R.id.quantityValue);
quantity.setText(number);
}
private void updatePrice(int number) {
TextView price = (TextView) findViewById(R.id.priceValue);
price.setText(NumberFormat.getCurrencyInstance().format(number * 5));
}
}
Here's my XML:
<TextView
android:text="Quantity"
android:id="#+id/quantityText"
android:textAllCaps="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#000000"
android:autoText="false"
android:paddingBottom="20dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_horizontal">
<TextView
android:text="0"
android:id="#+id/quantityValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:paddingBottom="16dp" />
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="-"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/quantityValue"
android:layout_toStartOf="#+id/quantityValue"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:onClick="decreaseQuantity" />
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="+"
android:id="#+id/button2"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/quantityValue"
android:layout_toEndOf="#+id/quantityValue"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:onClick="increaseQuantity" />
</RelativeLayout>
<TextView
android:text="Price"
android:id="#+id/priceText"
android:textAllCaps="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#000000"
android:autoText="false"
android:paddingTop="20dp" />
<TextView
android:text="$0"
android:id="#+id/priceValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:paddingBottom="20dp" />
<Button
android:text="Order"
android:textAllCaps="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
The formatting of the actual code is just fine, but it got screwy when I pasted in into stack, so I know that's not the problem.
Change you updateQuantity and updatePrice method like :
private void updateQuantity(int number) {
TextView quantity = (TextView) findViewById(
R.id.quantityValue);
quantity.setText(String.valueOf(number));
}
private void updatePrice(int number) {
TextView price = (TextView) findViewById(R.id.priceValue);
price.setText(String.valueOf(NumberFormat.getCurrencyInstance().format(number * 5)));
}
}
I'm making an android app for a simple calculator. The most important part is that it uses a "rolling" textView, so for example if you did 1+1, then if you pressed another arithmetic operator, "+" for example, the textView would then turn into 2+(enter new number, 3 for example) and then if you clicked multiply, it would then show 5* etc etc.
Right now the error is at line34 showing a null pointer error when I click the number 1 on the calculator. Wondering if anybody can assist me on this / other logic errors if you see them.
Basically if you click to use an arithmetic operator on the calculator, and if there's already an operator in the textView on the calculator, then it calculates the current textView and spits that new arithmetic operator beside it. Otherwise if there are no arithmetic operators on the calculator, it just puts the operator beside the first number.
What I'm trying to do is:
if the user clicks a number, then set rollingBar to that number
if the user clicks an operator
if it is the first operator then there must be only 1 string inside of
the rollingBar, so assign that string to leftNum, and assign the first
operator type to the text of the button eg. "1".
get text in rolling bar, assign it to text1, make text2 and assign
it to text1 + stringButton(which would be 1,2,3,4 etc.. name of btn.
set the first operator to false
if it's not the first operator, then you need to calculate by splitting
the function at the first arithmetic operator, assign the last number
from the split into rightNum
switch to the first operator type
do the math
assign this second operator to the next rollingBar
Right now I'm getting some problems... I'll show a picture and the java file...
Picture:
http://imgur.com/35hy4Ze
Java File:
package com.example.w0273754.calcfinal;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.lang.String;
public class MainActivity extends AppCompatActivity {
//declarations
TextView rollingBar;//where all of the text goes when the user clicks a button
String leftNum;
String rightNum;
boolean firstOptr;
String firstOptrType;
String number;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rollingBar = (TextView) findViewById(R.id.rollingBar);
leftNum = "";
rightNum = "";
firstOptr = true;
number = "";
}
public void number(View v){
Button button = (Button) v;
number += button.getText().toString();
rollingBar.setText(number);
}
//i can click 1 and it outputs 1
// i can click + and it outputs +
// when I click next number it just outputs the number, nothing else
public void operater(View v) {
Button button = (Button) v;
String stringButton = button.getText().toString();
//if it's the first operator being done, then set the rollbar text to text1,
// then add the operator string to text1 and store it in text 2.
if (firstOptr) {
leftNum = rollingBar.getText().toString();
firstOptrType = stringButton;
number += stringButton;
rollingBar.setText(number);
firstOptr = false;
}
//if it's not the first operator, then you need to calculate leftnum and the right num
else {
String fullString = (String) rollingBar.getText().toString();
String[] bits = fullString.split("[-+*/]");
rightNum = bits[bits.length - 1];
switch (firstOptrType) {
case "+":
add(leftNum, rightNum);
break;
case "-":
break;
case "*":
break;
case "/":
break;
default:
rollingBar.setText("OPERATOR METHOD PROBLEM");
}
}
}
public void add(String leftNum, String rightNum){
int parsedLeftNum = Integer.parseInt(leftNum);
int parsedRightNum = Integer.parseInt(rightNum);
rollingBar.setText(parsedLeftNum + parsedRightNum);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
XML file:
<TableLayout android:id="#+id/TableLayout"
tools:context=".MainActivity"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow android:paddingBottom="10dp" android:layout_height="match_parent" android:layout_width="match_parent">
<TextView android:id="#+id/rollingBar" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_column="1" android:layout_span="4" android:text="#string/rollingBar"/>
</TableRow>
<TableRow android:layout_height="match_parent"
android:layout_width="match_parent">
<Button android:id="#+id/buttonClear" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="#string/buttonClear" />
<Button android:id="#+id/buttonPlusMinus" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_span="1" android:text="#string/buttonPlusMinus" android:onClick="onClick1"/>
<Button android:id="#+id/buttonDelete" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_span="1" android:text="#string/buttonDelete" />
<Button android:id="#+id/buttonDivide" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_column="3" android:text="#string/buttonDivide" android:onClick="operater"/>
</TableRow>
<TableRow android:paddingBottom="10dp" android:layout_height="match_parent" android:layout_width="match_parent">
<Button android:id="#+id/button7" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="#string/button7" android:onClick="number" android:nestedScrollingEnabled="false"/>
<Button android:id="#+id/button8" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="#string/button8" android:onClick="number"/>
<Button android:id="#+id/button9" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="#string/button9" android:onClick="number"/>
<Button android:id="#+id/buttonMultiply" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_column="3" android:text="#string/buttonMultiply" android:onClick="operater"/>
</TableRow>
<TableRow android:paddingBottom="10dp" android:layout_height="match_parent" android:layout_width="match_parent">
<Button android:id="#+id/button4" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="#string/button4" android:onClick="number"/>
<Button android:id="#+id/button5" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="#string/button5" android:onClick="number"/>
<Button android:id="#+id/button6" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="#string/button6" android:onClick="number"/>
<Button android:id="#+id/buttonSubtract" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_column="3" android:text="#string/buttonSubtract" android:onClick="operater"/>
</TableRow>
<TableRow android:paddingBottom="10dp" android:layout_height="wrap_content" android:layout_width="match_parent">
<Button android:id="#+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_span="1" android:text="#string/button1" android:onClick="number"/>
<Button android:id="#+id/button2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_span="1" android:text="#string/button2" android:onClick="number"/>
<Button android:id="#+id/button3" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_span="1" android:text="#string/button3" android:onClick="number"/>
<Button android:id="#+id/buttonAdd" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_column="3" android:text="#string/buttonAdd" android:onClick="operater"/>
</TableRow>
<TableRow android:paddingBottom="10dp" android:layout_height="match_parent" android:layout_width="match_parent">
<Button android:id="#+id/button0" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_span="2" android:text="#string/button0" android:onClick="number"/>
<Button android:id="#+id/buttonDecimal" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_span="1" android:text="#string/buttonDecimal"/>
<Button android:id="#+id/buttonEqual" android:layout_height="match_parent" android:layout_width="wrap_content" android:layout_span="1" android:text="#string/buttonEqual" android:onClick="operater"/>
</TableRow>
<TableRow android:layout_height="match_parent" android:layout_width="match_parent">
<TextView android:id="#+id/textView" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="0"/>
</TableRow>
<TableRow android:layout_height="match_parent" android:layout_width="match_parent">
<TextView android:id="#+id/errorMessage" android:layout_height="wrap_content" android:layout_width="wrap_content"/>
</TableRow>
</TableLayout>
Thank you very much,for the help!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView rollingBar = (TextView) findViewById(R.id.rollingBar); // <-- HERE
String leftNum = "";
String rightNum = "";
boolean firstOptr = true;
String firstOptrType;
}
You declare a local variable rollingBar in onCreate which shadow the member variable of your Activity, so the member variable rollingBar always null, delete the TextView:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rollingBar = (TextView) findViewById(R.id.rollingBar);
String leftNum = "";
String rightNum = "";
boolean firstOptr = true;
String firstOptrType;
}
I have problem wit changing backgroud color in my project. I'm just trying to change list background color with condition in ViewBinder. But color of backgroung didn't changing.
My ViewBinder
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
LinearLayout ll = (LinearLayout) findViewById(R.id.listViewTopLinearLayout);
String voted = cursor.getString(VOTED_COLLUMN_INDEX);
if (columnIndex == cursor.getColumnIndex(AssetsTableHelper.COLUMN_VOTED)) {
boolean is_checked = voted.equals("true");
if (is_checked) {
((View) ll.getParent()).setBackgroundResource(R.color.votedColor);
((View) view.getParent()).setBackgroundResource(R.color.votedColor);
} else {
((View) view.getParent()).setBackgroundResource(R.color.notVotedColor);
}
return true;
}
return false;
};
onCreate
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
integrator = new IntentIntegrator(this);
datasource = new AssetsDataSource(this);
Cursor cursor = datasource.getCursorForAllAssets();
adapter = new SimpleCursorAdapter(this, R.layout.listview_item_row,cursor, UI_BINDING_FROM,UI_BINDING_TO, 0);
adapter.setViewBinder(new CustomViewBinder());
setListAdapter(adapter);
List_item_row.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tvSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="#000000"
android:layout_marginLeft="5dp"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvHomeNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
I think, problem is in argument of setBackgroundResource(R.color.votedColor). Argument should be a resource id for example some R.drawable.votedColor.
Take a look setBackgroundResource and to make a drawable xml file take a look at Drawable Resource.