java.lang.ClassCastException only on Android 2.3? - java

When i run my app on Android 2.3 devices.I am getting an java.lang.ClassCastException: android.widget.ImageView .But it works perfect in other Devices.
If i Comment the below Lines it is working.But i can't find any Error's in these Lines.
Any Suggestions ???
up = (Button) findViewById(R.id.up);
down = (Button) findViewById(R.id.down);
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
up.setOnClickListener(this);
down.setOnClickListener(this);
start.setOnClickListener(this);
stop.setOnClickListener(this);
Activity
public class Presentation extends MainActivity implements ControllerDroidActionReceiver, OnClickListener
{
Button up, down, start, stop;
private ControllerDroid application;
private ControlView controlView;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.application = (ControllerDroid) this.getApplication();
ActionBar ab = getSupportActionBar();
ab.setTitle("Presentation");
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.presentation, null, false);
mDrawer.addView(contentView, 0);
up = (Button) findViewById(R.id.up);
down = (Button) findViewById(R.id.down);
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
up.setOnClickListener(this);
down.setOnClickListener(this);
start.setOnClickListener(this);
stop.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
if (v == up)
{
application.sendAction(new KeyboardAction(19));
// Log.i("Button", "UP");
}
else if (v == down)
{
application.sendAction(new KeyboardAction(20));
}
else if (v == start)
{
application.sendAction(new KeyboardAction(26));
// Log.i("Button", "STARTED PPT");
}
else if (v == stop)
{
application.sendAction(new KeyboardAction(23));
}
}
#Override
protected void onResume()
{
super.onResume();
this.application.registerActionReceiver(this);
}
protected void onPause()
{
super.onPause();
this.application.unregisterActionReceiver(this);
}
public void receiveAction(ControllerDroidAction action)
{
if (action instanceof ScreenCaptureResponseAction)
{
this.controlView.receiveAction((ScreenCaptureResponseAction) action);
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:text="UP" />
<Button
android:id="#+id/down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:text="DOWN" />
<Button
android:id="#+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:text="start" />
<Button
android:id="#+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:text="Stop" />
</LinearLayout>

//put setContentview(R.layout.presentation)
or do it as below
up = (Button)contentView. findViewById(R.id.up);
down = (Button)contentView. findViewById(R.id.down);
start = (Button)contentView. findViewById(R.id.start);
stop = (Button)contentView. findViewById(R.id.stop);

Please try with contentView.findViewById() because you are not doing setContentView().
up = (Button) contentView.findViewById(R.id.up);
down = (Button) contentView.findViewById(R.id.down);
start = (Button) contentView.findViewById(R.id.start);
stop = (Button) contentView.findViewById(R.id.stop);
May be some where you used at least one of them id for ImageView.
It may help you..

Related

My android app stopping when I touch the button

This is my code guys when I touch the "saldir" button or another button my app
crashes and this is my app's image https://ibb.co/ce3ATm
This is my error (https://ibb.co/jBxb16):
Dergilik APP has stopped
package com.example.rg.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener
{
int sayac =0;
TextView tv;
**//kind of turkish words don't focus the variables**
TextView tvkarekterOzellikleri;
Button byemekye;
#Override
**//THIS IS MY MAIN_Activity.Java**
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.Answer);
//I'm trying very basic app when I touch button this layout'll changed
tvkarekterOzellikleri = (TextView) findViewById(R.id.Question);
Button bsaldir = (Button) findViewById(R.id.saldir);
//comment/i have 3 buttons as you can see on link at top
Button buyu = (Button) findViewById(R.id.uyu);
Button byemekye = (Button) findViewById(R.id.yemekYe);
bsaldir.setOnClickListener(this);
buyu.setOnClickListener(this);
byemekye.setOnClickListener(this);*
//This part useless now because I didn't added this part later I'll add this part
//Character k = new Character();
//k.Movementnum = 10;
//k.Kilos = 10;
//k.FightPower = 10;
//}
#Override
//This part is PROBLEM WHEN I DELETE THIS PART MY CODE IS WORKING
public void onClick(View v){
if (v.getId()==byemekye.getId())
tv.setText("yemek yenildi");
else
tv.setText("Nar" + (sayac++));
}
}
When I delete if.(v.getId....) app is running normal.
This side is my activity_main.xml
<?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="com.example.rg.myapplication.MainActivity">
<Button
android:id="#+id/saldir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:elevation="0dp"
android:text="saldir" />
<TextView
android:id="#+id/Question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|bottom|center"
android:padding="3dp"
android:text="#string/Dilektasi"
android:textAppearance="#style/TextAppearance.AppCompat.Display2"
android:layout_above="#+id/saldir"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="75dp" />
<TextView
**android:id="#+id/Answer**"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/saldir"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:text="" />
//I have 3 button for my app as you can see on image
<Button
android:id="#+id/yemekYe"
**im saying twice pls dont focus on variables**
**next time i'll make this variables english**
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/saldir"
android:layout_toLeftOf="#+id/saldir"
android:layout_toStartOf="#+id/saldir"
android:text="yemek ye" />
smth smth smth smth
<Button
android:id="#+id/uyu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/saldir"
android:layout_toEndOf="#+id/saldir"
android:layout_toRightOf="#+id/saldir"
android:text="Uyu" />
//dont focus the variable names
</RelativeLayout>
I think your problem is this:
Define Buttons:
private Button bt;
Set id of the button on xml, than on onCrate()
bt= (Button) findViewById(R.id.button1); //button1 is the id of the button
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do actions
}
});
code:
public class YourActivity extends AppCompatActivity {
private Button btn1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_details);
parent_view = findViewById(android.R.id.content);
btn1= (Button) findViewById(R.id.button1); //button1 is the id of the button
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do actions
}
});
}
on xml
<Button
android:id="#+id/butto1" <!-- the id -->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />

How Can I initialize my variables from another layout?

After much research, I have been unable to figure out how I can initialize my TextView in my java file in Android Studio. The TextView in question is located in a different layout file so I don't know the correct syntax to use. I think my question is similar to: Null pointer Exception on .setOnClickListener
But the solution for him is not working for me.
Here is my troublesome code:
Microsoft = (Button) findViewById(R.id.Microsoft);
Microsoft.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Wrong!",
Toast.LENGTH_SHORT).show();
//TextView score = (TextView) findViewById(R.id.score);
TextView score = (TextView)
score.findViewById(R.id.question2);
(score).setText(0);
}
});
TheFindViewById part is the part I need.
FULL CODE VVVV
package org.flinthill.finalprojectv3;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.text.method.DigitsKeyListener;
import android.text.InputFilter;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button SuSe;
Button DOS;
Button B;
Button BIOS;
Button Microsoft;
Button LenBosackandSandyLerner;
Button HaskelDiklah;
Button SteveWozniak;
SuSe = (Button) findViewById(R.id.SuSe);
SuSe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Wrong!",
Toast.LENGTH_SHORT).show();
TextView score = (TextView) findViewById(R.id.score);
(score).setText(0);
}
});
DOS = (Button) findViewById(R.id.DOS);
DOS.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Wrong!",
Toast.LENGTH_SHORT).show();
TextView score = (TextView) findViewById(R.id.score);
(score).setText("0");
}
});
B = (Button) findViewById(R.id.B);
B.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Wrong!",
Toast.LENGTH_SHORT).show();
TextView score = (TextView) findViewById(R.id.score);
(score).setText("0");
}
});
BIOS = (Button) findViewById(R.id.BIOS);
BIOS.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Right!",
Toast.LENGTH_SHORT).show();
TextView score = (TextView) findViewById(R.id.score);
(score).setText("1");
setContentView(R.layout.question2);
}
});
//QUESTION 2
Microsoft = (Button) findViewById(R.id.Microsoft);
Microsoft.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Wrong!",
Toast.LENGTH_SHORT).show();
//TextView score = (TextView) findViewById(R.id.score);
TextView score = (TextView)
score.findViewById(R.id.question2);
(score).setText(0);
}
});
/*LenBosackandSandyLerner = (Button)
findViewById(R.id.LenBosackandSandyLerner);
LenBosackandSandyLerner.setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Right!",
Toast.LENGTH_SHORT).show();
TextView score = (TextView) findViewById(R.id.score);
(score).setText("2");
setContentView(R.layout.question3);
}
});
HaskelDiklah = (Button) findViewById(R.id.HaskelDiklah);
HaskelDiklah.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Wrong!",
Toast.LENGTH_SHORT).show();
TextView score = (TextView) findViewById(R.id.score);
(score).setText("0");
}
});
SteveWozniak = (Button) findViewById(R.id.SteveWozniak);
SteveWozniak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Wrong!",
Toast.LENGTH_SHORT).show();
TextView score = (TextView) findViewById(R.id.score);
(score).setText("0");
}
});*/
}
}
XML CODE:
LAYOUT 1:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/android"
tools:context="org.flinthill.finalprojectv3.MainActivity">
<TextView
android:layout_width="wrap_content"
android:id="#+id/LUL"
android:textColor="#color/LightGreen"
android:layout_height="wrap_content"
android:typeface="serif"
android:text="Which is NOT an OS?"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:textSize="24sp"/>
<Button
android:id="#+id/SuSe"
android:onClick="SuSeClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SuSe"
android:layout_marginTop="100dp"
android:layout_below="#+id/LUL"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/BIOS"
android:onClick="BIOSClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BIOS"
android:layout_below="#+id/SuSe"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/DOS"
android:onClick="DOSClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DOS"
android:layout_below="#+id/BIOS"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/B"
android:onClick="BClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
android:layout_below="#+id/DOS"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/score"
android:textColor="#color/colorAccent"
android:text="0"
android:textSize="32dp"
android:layout_below="#+id/LUL"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp" />
</RelativeLayout>
LAYOUT 2:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/question2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/android"
tools:context="org.flinthill.finalprojectv3.MainActivity">
<TextView
android:layout_width="wrap_content"
android:id="#+id/question2text"
android:textColor="#color/LightGreen"
android:layout_height="wrap_content"
android:typeface="serif"
android:text="Who created Cisco"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:textSize="24sp"/>
<Button
android:id="#+id/Microsoft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Microsoft"
android:layout_marginTop="100dp"
android:layout_below="#+id/question2text"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/LenBosackandSandyLerner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Len Bosack and Sandy Lerner"
android:layout_below="#+id/Microsoft"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/HaskelDiklah"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Haskel Diklah"
android:layout_below="#+id/LenBosackandSandyLerner"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/SteveWozniak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Steve Wozniak"
android:layout_below="#+id/HaskelDiklah"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/score"
android:textColor="#color/colorAccent"
android:text="1"
android:textSize="32dp"
android:layout_below="#+id/question2text"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp" />
</RelativeLayout>
//Change the name id in the xml file
Button microsoftButton = (Button) findViewById(R.id.microsoftButton);
TextView scoreTextView = (TextView) findViewById(R.id.scoreTextView);
microsoftButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Wrong!", Toast.LENGTH_SHORT).show();
// This was declared above so now you can use it. You can only set it to a String not to an Integer.
scoreTextView.setText("0");
}
});
You cannot do this unless your TextView is within the layout your Activity, Fragment or Dialog controls.
The findViewById method looks for Views within the layout previously configured by the setContentView(layout) or the layout inflated in your Fragment or Dialog. If it does not find anything, your TextView will have a null reference.

visibility gone of image view when come from 2nd activity to 1st activity using putextra and getextra method in android

everyone. I am new in android. I am creating two activities in android ,1st activity has two image view and one button . and 2nd activity has one button. when I go to second activity and then from second activity agian . one image view of 1st activity should visibility gone. how can i do this.
here is my code
Activitymain.xml
<ImageView
android:id="#+id/1stimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/2ndimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to second activity" />
mainactivity.java
ImageView imageView1, imageView2;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = (ImageView) findViewById(R.id.1stimage);
imageView2 = (ImageView) findViewById(R.id.2ndimage);
btn = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent Firstintent= new Intent(MainActivity.this,SecondActivity.class);
startActivity(Firstintent);
}
});
}
secondactivity.xml
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to 1st activity" />
secondactivity.java
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
btn = (Button) findViewById(R.id.btn2);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent SecondIntent = new Intent(SecondActivity.this,
MainActivity.class);
SecondActivity.this.startActivity(SecondIntent);
SecondActivity.this.finishActivity();
}
});
}
please help me
Activitymain.xml
<ImageView
android:id="#+id/1stimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/2ndimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to second activity" />
mainactivity.java
ImageView imageView1, imageView2;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = (ImageView) findViewById(R.id.1stimage);
imageView2 = (ImageView) findViewById(R.id.2ndimage);
btn = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent Firstintent= new Intent(MainActivity.this,SecondActivity.class);
startActivity(Firstintent);
}
});
Bundle extras = getIntent().getExtras();
if(extras != null){
if(extras.getBoolean("isResult", false)){
imageView1.setVisability(View.GONE);
}
}
}
secondactivity.xml
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to 1st activity" />
secondactivity.java
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
btn = (Button) findViewById(R.id.btn2);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent SecondIntent = new Intent(SecondActivity.this,
MainActivity.class);
SecondIntent.putExtra("isResult", true);
SecondActivity.this.startActivity(SecondIntent);
SecondActivity.this.finishActivity();
}
});
}
Use putextra(tag, some variable to use in mainActivity) and Bundle extras = getIntent().getExtras(); extras.get[type of variable](tag) to pass a variable (probably a boolean) to tell the onCreate() to show or not show the pic.
EDIT:
Here is one way to implement it: in the first activity:
1) Check if there are extras (A.K.A. if second activity was loaded/ there is a true)
2) If (yes) { if (first pic is loaded) { remove first pic } else if (first pick is not but second is loaded) { remove second pic } ... for all the pictures you want to do this to.
Remember you only need to pass true/false from the second activity (and then handle it in the first/one being modified)

Changing ImageView size

I'm trying to have an image show up when the name of it (TextVIew) is clicked. I have the code set up to what I believe is the right thing but the app force closes when I launch it. This is just a test code, so its not my actual app, but the idea is the same so can someone help me figure out why this is crashing?
java file:
TextView text = (TextView) findViewById(R.id.text);
ImageView image = (ImageView) findViewById(R.id.image);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
image.getLayoutParams().height=20;
}
});
}
xml file:
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:gravity="center"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:text="show image" />
You have to have this code (below) inside onCreate()
TextView text = (TextView) findViewById(R.id.text);
ImageView image = (ImageView) findViewById(R.id.image);
Try it.
Update:
The code could look like this:
public class MainActivity extends Activity {
ImageView image = null;
TextView text = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView) findViewById(R.id.text);
image = (ImageView) findViewById(R.id.image);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (image.getVisibility() == View.GONE) {
image.setVisibility(View.VISIBLE);
text.setText("hide the image");
} else {
image.setVisibility(View.GONE);
text.setText("show the image");
}
}
});
}
}
and layout is here:
<LinearLayout 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:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:visibility="gone" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show the image" />
</LinearLayout>
Fill your elements in onCreate, not as a field, after setting the content view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text = (TextView) findViewById(R.id.text);
ImageView image = (ImageView) findViewById(R.id.image);
...
And try setting the size of the ImageView not directly from layoutparams but with setHeight/setWidth functions

TextView isn't updating in Android

I'm making a simple "novelty" app that counts how many sneezes are done, it's more of a fun app that builds up experience until I do my huge project during the summer. I have two buttons, one adds a sneeze and the other clears how many sneezes there currently are. It holds the highest number of sneezes that there were previously. The problem is, the TextViews never update, they only initialize to zero. I used a Toast.makeText() to make sure that the buttons are working (they are). Any help is appreciated. Thanks
Java code:
public class MainActivity extends ActionBarActivity {
private int record_number = 0;
private int current_number = 0;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
Button add_one = (Button) findViewById(R.id.addone);
Button clear_1 = (Button) findViewById(R.id.clear);
add_one.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
current_number += 1;
Toast.makeText(MainActivity.this, "Button Clicked " + current_number, Toast.LENGTH_SHORT).show();
}
});
clear_1.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
current_number = 0;
Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
}
});
TextView rec_text = (TextView) findViewById(R.id.record_num);
TextView cur_text = (TextView) findViewById(R.id.current_num);
if (current_number >= record_number)
{
record_number = current_number;
}
rec_text.setText(String.valueOf(record_number));
cur_text.setText(String.valueOf(current_number));
}
}
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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.michail.sneezecounter.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Sneeze Counter"
android:id="#+id/title"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Record Number of Sneezes:"
android:id="#+id/textView"
android:layout_below="#+id/title"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add one Sneeze"
android:id="#+id/addone"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="76dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear Current Number of Sneezes"
android:id="#+id/clear"
android:layout_marginBottom="13dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/record_num"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:singleLine="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Current Number of Sneezes:"
android:id="#+id/currentLabel"
android:layout_centerVertical="true"
android:layout_alignRight="#+id/textView"
android:layout_alignEnd="#+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_below="#+id/currentLabel"
android:layout_alignLeft="#+id/record_num"
android:layout_alignStart="#+id/record_num"
android:layout_marginTop="21dp"
android:id="#+id/current_num" />
</RelativeLayout>
You need to update the text of the TextViews inside the onClickListeners. In fact, all your logic for counting, clearing, and recording the record needs to be done in your onClickListeners (or methods called by them). Right now you only do it once in onCreate, then never again. You can do this in onCreate:
final TextView cur_text = (TextView) findViewById(R.id.current_num);
add_one.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
current_number += 1;
cur_text.setText(Integer.toString(current_number);
}
});
And similar for the other TextView & onClickListener.
You only set the contents of your TextViews once. You should update them every time you get a click event. Specifically:
add_one.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
current_number += 1;
if (current_number >= record_number)
{
record_number = current_number;
rec_text.setText(String.valueOf(record_number));
}
cur_text.setText(String.valueOf(current_number));
}
});
clear_1.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
current_number = 0;
cur_text.setText(String.valueOf(current_number));
}
});
NOTE: if your variables current_number, record_number, cur_text, and rec_text aren't already declared as class member variables, you'll want to do that do that so that they're accessible once you leave the scope of the method you're doing all this in (I assume it's onCreate(...).
What you are going to need to do here is update the labels during the on click events of the button. You currently only update them on activity create. This doesn't execute every time there is a click. Can I answer any questions about the fixed up version below?
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
final TextView rec_text = (TextView) findViewById(R.id.record_num);
final TextView cur_text = (TextView) findViewById(R.id.current_num);
Button add_one = (Button) findViewById(R.id.addone);
Button clear_1 = (Button) findViewById(R.id.clear);
add_one.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
current_number += 1;
if (current_number >= record_number)
record_number = current_number;
cur_text.setText(String.valueOf(current_number));
rec_text.setText(String.valueOf(record_number));
}
});
clear_1.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
current_number = 0;
cur_text.setText(String.valueOf(current_number));
rec_text.setText(String.valueOf(record_number));
}
});
cur_text.setText(String.valueOf(current_number));
rec_text.setText(String.valueOf(record_number));
}

Categories