i am having error with open and closing keys - java

I am creating a profile for my user but i dont know why i am getting this error
pls can you look ate the code and tell me what is wrong thank you very much
public class Profile extends AppCompatActivity {
EditText editText;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile2);
editText =(EditText)findViewById(R.id.nome);
imageView=(ImageView) findViewById(R.id.foto);
imageView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
}
};
}
}

Try this:
public class Profile extends AppCompatActivity {
EditText editText;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile2);
editText =(EditText)findViewById(R.id.nome);
imageView=(ImageView) findViewById(R.id.foto);
imageView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
}
});
// the closing parathesis ')' of setOnClickListener was missing
}
}

Related

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(){
}
}

Load image from URL on button click

I have this ImageView that displays a picture from a URL. I want to have a button that when clicked will display the image from the URL. Could anyone teach me how to do this?
Here's my code:
String url = "https://www.google.com.ph/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&ved=0ahUKEwiGzJm-l5PWAhVJl5QKHegTAg0QjBwIBA&url=https%3A%2F%2Fwww.enterprise.co.uk%2Fcontent%2Fdam%2Fglobal-vehicle-images%2Fcars%2FVAUX_INSI_2014.png&psig=AFQjCNGerQpF4NHcx50OFhQ2AGUlJYQCpQ&ust=1504877444951754";
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.imageView);
loadImageFromUrl(url);
}
private void loadImageFromUrl(String url) {
Picasso.with(this) .load(url) .placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.into(imageView, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
}
});
}
Picasso:
compile 'com.squareup.picasso:picasso:2.5.2'
Called when a view has been clicked.
imageView = (ImageView)findViewById(R.id.imageView);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do something here
loadImageFromUrl(url);
}
});
Add a button to your xml file. Let it be with id: btn_load;
Inside the activity add
Button btn = findViewById(R.id.btn_load);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
loadImageFromUrl(url);
}
});
Create a button in your xml layout file. Then in onCreate call:
final String url = "https://www.google.com.ph/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&ved=0ahUKEwiGzJm-l5PWAhVJl5QKHegTAg0QjBwIBA&url=https%3A%2F%2Fwww.enterprise.co.uk%2Fcontent%2Fdam%2Fglobal-vehicle-images%2Fcars%2FVAUX_INSI_2014.png&psig=AFQjCNGerQpF4NHcx50OFhQ2AGUlJYQCpQ&ust=1504877444951754";
ImageView imageView;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.imageView);
button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
loadImageFromUrl(url);
}
});
}
Note: for this to work you will have to declare your url String as final, because you're using it inside an anonymous class (OnClickListener).
Note2: The image url you have may not work. Use this one instead: https://www.enterprise.co.uk/content/dam/global-vehicle-images/cars/VAUX_INSI_2014.png

Android App button press not functioning correctly

Being brand new to Java I'm not sure why this when called with android:onClick="changeInfo" isn't changing the text view back to the original value.
public class JavaTest extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_java_test);
}
public void changeInfo(View view){
setContentView(R.layout.activity_java_test);
TextView t = (TextView)findViewById(R.id.textSpace1);
if (t.getText()==getResources().getString(R.string.lorem_ipsum)){
t.setText("[Email Here]");
}
else{
t.setText(getResources().getString(R.string.lorem_ipsum));
}
}
Could anyone help, thank you.
Try removing setContentView(R.layout.activity_java_test); from the changeInfo(View view) method.
setContentView(R.layout.activity_java_test);
You don't have to call setContentView inside changeInfo method
t.getText()==getResources().getString(R.string.lorem_ipsum)
it should be t.getText().toString().equeals(getResources().getString(R.string.lorem_ipsum))
Try this
public class JavaTest extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_java_test);
}
public void changeInfo(View view){
TextView t = (TextView)findViewById(R.id.textSpace1);
if (t.getText().toString().equeals(getResources().getString(R.string.lorem_ipsum))){
t.setText("[Email Here]");
}
else{
t.setText(getResources().getString(R.string.lorem_ipsum));
}
}

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.OutOfMemoryError

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.

Categories

Resources