Android Studio unable to resolve method in class - java

I am trying to make a basic connect 3 app as part of a course challenge.
The problem I am having is that I am unable to access the methods inside the class Players in order to set which buttons have been pressed by which players: Cannot resolve method 'noughtsPlayer'.
I created an instance of Players on the onCreate method and declared it at the top of the script. That instance itself is recognised but for some reason the contained methods, noughtsPlayer and crossesPlayer are not.
MainActivity.java
package com.example.richardcurteis.connect3;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public boolean noughtsTurn;
ArrayList board;
String players;
public void receiveClick(View view) {
String buttonPressed = (String) view.getTag();
board.remove(buttonPressed);
if (view instanceof Button) {
Button B = (Button) view;
B.setText(noughtsTurn ? players.noughtsPlayer() : players.crossesPlayer());
}
}
class Players {
public String noughtsPlayer() { return "O"; }
public String crossesPlayer() { return "X"; }
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
boolean noughtsTurn = true;
board = new ArrayList();
for (int x = 0; x < getBoardSize(); x++) {
String y = String.valueOf(x);
board.add(y);
}
Players players = new Players();
}
public int getBoardSize() {
int buttonCount = 0;
TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);
for (int rowIndex = 0; rowIndex < tableLayout.getChildCount(); rowIndex++) {
View tableLayoutChild = tableLayout.getChildAt(rowIndex);
if (tableLayoutChild instanceof TableRow) {
for (int i = 0; i < ((ViewGroup) tableLayoutChild).getChildCount(); i++) {
View view = ((ViewGroup) tableLayoutChild).getChildAt(i);
if (view instanceof Button) {
buttonCount++;
}
}
}
}
return buttonCount;
}
#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);
}
}

You have global variable String players and local variable Players players.
This is problem.
And class String doesn't have those methods.
To solve this problem change String players; to Players players;
and Players players = new Players(); to players = new Players();

Related

Android Text input always give me NaN value

I can not figure out why this code output always show NaN. Is there anybody who can help me figure out where I did mistake?
package com.example.user.solar_calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;
public class efficiency extends Activity {
Button button;
EditText efficiency;
EditText vol;
String e,v;
double e1,wat,v1, AC_Load_in_ampere_hour_calculation,dc;
double load,load_vol,Total_Daily_load,lol;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_efficiency);
initControls();
addListenerOnButton();
}
public void initControls() {
efficiency=(EditText)findViewById(R.id.editText1);
vol=(EditText)findViewById(R.id.editText2);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
e1=Double.parseDouble(efficiency.getText().toString()); // Make use of autoboxing. It's also easier to read.
} catch (NumberFormatException e) {
e1=1;
}
try {
v1=Double.parseDouble(vol.getText().toString());// Make use of autoboxing. It's also easier to read.
} catch (NumberFormatException e) {
v1=1;
}
Bundle extras = getIntent().getExtras();
if(extras !=null) {
String value = extras.getString("FOUR");
load=Double.parseDouble(value.toString());
}
wat=load_vol*(1+.1);
AC_Load_in_ampere_hour_calculation = (load)/( v1* (e1/100));
Total_Daily_load=AC_Load_in_ampere_hour_calculation+dc;
Intent i = new Intent(efficiency.this, calculation.class);
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString("seven", String.valueOf(AC_Load_in_ampere_hour_calculation));
bundle.putString("eight", String.valueOf(Total_Daily_load));
bundle.putString("nine", String.valueOf(dc));
//Add the bundle to the intent
i.putExtras(bundle);
//Fire that second activity
startActivity(i);
}
});
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
}
#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);
}
}
I think the line who cause problem is AC_Load_in_ampere_hour_calculation = (load)/( v1* (e1/100));
Because if v1 = 0 it will be impossible.

Issue with a simple app

I'm new in coding, and I was trying to make a simple app that make a subtraction from 2 variable, but when I click the button to calculate, the app crashes... Can someone help me finding the problem in the code? (Android Studio)
Here's the code:
package apps.ste.resto;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import static apps.ste.resto.R.id.risultato;
public class MainActivity extends AppCompatActivity {
TextView importo;
TextView conto;
TextView risultato;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
TextView risultato = (TextView) findViewById(R.id.textView);
TextView importo = (TextView) findViewById(R.id.editText);
TextView conto = (TextView) findViewById(R.id.editText2);
Button bottone = (Button) findViewById(R.id.button);
bottone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result();
}
});
}
public void result(){
float result = Float.parseFloat(importo.getText().toString()) - Float.parseFloat(conto.getText().toString());
risultato.setText(Float.toString(result));
}
#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);
}
I think you are getting null value for getText()..
SO you can try this :
public void result(){
boolean digitsOnly1 = TextUtils.isDigitsOnly(importo.getText()+"");
boolean digitsOnly2 = TextUtils.isDigitsOnly(conto.getText()+"");
if(digitsOnly1 && digitsOnly2){
float result = Float.parseFloat(importo.getText().toString()) - Float.parseFloat(conto.getText().toString());
risultato.setText(result+""));
}else{
risultato.setText("Please provide inputs");
}
}

app not displaying x and y coordinates

I m a beginner to android so i made app to display just simple x and y coordinates.
package company.com.bucky;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MotionEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public static int X;
public static int Y;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final TextView firsttextview = (TextView)findViewById(R.id.textView);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
firsttextview.setText("X : " + X + "\nY : " + Y);
}
public boolean onTouch(View v, MotionEvent event) {
this.Y = (int) event.getX();
this.Y = (int) event.getY();
return true;
}
#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);
}
}
SO when i run the app i just shows the value of x and y just 0..and no change in value when i touch the screen.Thx in advance for helping
Your onTouch handler only sets the static variables, but does not update the text view.
Put firsttextview.setText("X : " + X + "\nY : " + Y);
into the onTouch handler.

Shared element transition with picasso not working even after implementing callback

The image moves perfectly the first time into its place i.e detail activity image view and also returns perfectly back to the main activity but when I click the same image next time the transition animation moves the image to an incorrect (too high) offset in the detail activity and once the animation completes the image will appear to "warp" into the correct position.
Here is my DetailActivity.java file:
package com.akshitjain.popularmovies;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
public class DetailActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
postponeEnterTransition();
}
ImageView backdropImageView = (ImageView) findViewById(R.id.detail_image_view);
final ImageView posterImageView = (ImageView) findViewById(R.id.detail_poster);
TextView overviewTextView = (TextView) findViewById(R.id.overview_text_view);
TextView releaseDateTextView = (TextView) findViewById(R.id.release_date);
TextView userRatingTextView = (TextView) findViewById(R.id.user_rating);
TextView genreTextView = (TextView) findViewById(R.id.genre);
genreTextView.setText("");
Genre genreObject = new Genre();
String genreName;
Intent intent = getIntent();
if (intent != null && intent.hasExtra(Constants.MOVIE_OBJECT_PARCELABLE_EXTRA)) {
Movies movies = intent.getParcelableExtra(Constants.MOVIE_OBJECT_PARCELABLE_EXTRA);
int[] genre;
genre = movies.genre;
for (int i = 0; i < genre.length; ++i) {
genreName = genreObject.getGenreName(genre[i]);
genreTextView.append(genreName);
if (i < genre.length - 1) {
genreTextView.append(", ");
}
}
setTitle(movies.originalTitle);
Picasso.with(getApplicationContext())
.load((Constants.IMAGE_BASE_URL + Constants.POSTER_SIZE_LARGE).trim() + movies.backdropPath)
.into(backdropImageView);
Picasso.with(getApplicationContext())
.load((Constants.IMAGE_BASE_URL + Constants.POSTER_SIZE_SMALL).trim() + movies.posterPath)
.into(posterImageView, new Callback() {
#Override
public void onSuccess() {
posterImageView.getViewTreeObserver().addOnPreDrawListener(
new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
posterImageView.getViewTreeObserver().removeOnPreDrawListener(this);
startPostponedEnterTransition();
}
return true;
}
}
);
}
#Override
public void onError() {
}
}
);
overviewTextView.setText(movies.overview);
releaseDateTextView.setText(movies.releaseDate);
userRatingTextView.setText(movies.userRating);
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#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_detail, 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();
switch (id) {
case android.R.id.home:
supportFinishAfterTransition();
return true;
case R.id.action_settings:
return true;
}
return super.onOptionsItemSelected(item);
}
}
Try this.
posterImageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Picasso.with(getApplicationContext())
.load((Constants.IMAGE_BASE_URL + Constants.POSTER_SIZE_SMALL).trim() + movies.posterPath)
.into(posterImageView, new Callback() {
#Override
public void onSuccess() {
startPostponedEnterTransition();
}
#Override
public void onError() {
}
});
}
}

Counter not incrementing for radio button in android

am using an if statement to increment the answer for a quiz app in android. i think for loop or while loop should work berrter. But i hae also tested that . am now getting it well.
Below is my code. Thank you
package newton.quizapplication;
import android.app.Activity;
import android.database.CursorJoiner;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MyActivity extends Activity {
private RadioGroup radioGroup1;
private RadioGroup radioGroup2;
private RadioGroup radioGroup3;
private RadioButton radioButton1;
private RadioButton radioButton2;
private RadioButton radioButton3;
private Button btnSubmit;
private int Ans = 0;
private TextView Total;
// private int wrong = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
String All;
Total = (TextView)findViewById(R.id.Score);
addListenerOnButton();
}
public void addListenerOnButton() {
radioGroup1 = (RadioGroup) findViewById(R.id.rg1);
radioGroup2 = (RadioGroup) findViewById(R.id.rg2);
radioGroup2 = (RadioGroup) findViewById(R.id.rg3);
btnSubmit = (Button) findViewById(R.id.submit);
btnSubmit.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId1 = radioGroup1.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton1 = (RadioButton) findViewById(selectedId1);
// get selected radio button from radioGroup
int selectedId2 = radioGroup1.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton2 = (RadioButton) findViewById(selectedId2);
// get selected radio button from radioGroup
int selectedId3 = radioGroup1.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton3 = (RadioButton) findViewById(selectedId3);
if (radioButton1.getText().equals("Hyper Text Markup Language")) {
Ans++;
}
else if (radioButton2.getText().equals("The World Wide Web Consortium")) {
// Toast.makeText(MyActivity.this, "Incorrect Answer", Toast.LENGTH_SHORT).show();
Ans++;
}
else if (radioButton2.getText().equals("Specific places within a particular page")) {
Ans++;
}
Total.setText("You Got " + Ans + " Answer correct out of 3");
Ans =0;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, 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);
}
}
You should change your conditions to :
if (radioButton1.getText().equals("Hyper Text Markup Language")) {
Ans++;
}
if (radioButton2.getText().equals("The World Wide Web Consortium")) {
// Toast.makeText(MyActivity.this, "Incorrect Answer", Toast.LENGTH_SHORT).show();
Ans++;
}
if (radioButton3.getText().equals("Specific places within a particular page")) {
Ans++;
}
In your original code of if (...) ... else if (...) ... else if (...) ..., only one condition can be met (since the first time a condition evaluates to true, no other conditions would be checked), so Ans can only be 0 or 1.
In addition, I'm assuming your last if should check radioButon3.

Categories