android hiding ui and showing ui code not working [duplicate] - android

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 6 years ago.
package com.example.eiraj.listviewseefrgmentsfiirst;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView= (TextView) findViewById(R.id.textView);
public void show(View view){
textView.setVisibility(View.VISIBLE);
}
public void hide(View view){
textView.setVisibility(View.INVISIBLE);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
here two buttons are being used to show data and hide data but whenever I try to run it emulator shows the message unfortunately uihide stopped also I downloaded the apk to run it on my phone but same message came there

Move TextView textView= (TextView) findViewById(R.id.textView); inside onCreate
while keeping textView reference outside onCreate because before the execution of onCreate there is no layout attached to your activity , hence the issue
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView= (TextView) findViewById(R.id.textView);
}
Then your MainActivity.java will be
public class MainActivity extends AppCompatActivity {
TextView textView;
public void show(View view){
textView.setVisibility(View.VISIBLE);
}
public void hide(View view){
textView.setVisibility(View.INVISIBLE);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView= (TextView) findViewById(R.id.textView);
}
}
Plus you can also use textView.setVisibility(View.GONE); if you want to completely make your view invisible

Attach your TextView inside onCreate():
package com.example.eiraj.listviewseefrgmentsfiirst;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView= (TextView) findViewById(R.id.textView);
}
public void show(View view){
textView.setVisibility(View.VISIBLE);
}
public void hide(View view){
textView.setVisibility(View.INVISIBLE);
}
}

We can't initialize Views before setContentView() is called and it is compulsory to call setContentView() inside onCreate() method.
Do like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView= (TextView) findViewById(R.id.textView);
}

Related

issue with my first android app

before posting my question , i wanna clarify that is my first post on stackoverflow and let's the story beggin.
as the title said , i'm making my first app on android and i found myself blocked with an issue .
there is 3 button on my app :
button1 : give textview2 "hello world again " and make it VISIBLE
// button2 : make textView2 INVISIBLE
// button3 : make textView1 INVISIBLE
this is the code freom main_activity :
package com.example.ismail.app_test_1;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
Button button_aff;
Button button_hide;
Button button_hide_hw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_aff = (Button) findViewById(R.id.button);
button_hide = (Button) findViewById(R.id.button2);
button_hide_hw = (Button) findViewById(R.id.button3);
button_aff.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Show("hello world again");
}
});
button_hide.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Hide();
}
});
button_hide_hw.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Hide_hw();
}
});
}
public void Show(String str)
{
TextView text;
text = (TextView) findViewById(R.id.textView2);
text.setVisibility(View.VISIBLE);
text.setText(str);
setContentView(text);
}
public void Hide()
{
TextView text;
text = (TextView) findViewById(R.id.textView2);
text.setVisibility(View.INVISIBLE);
setContentView(text);
}
public void Hide_hw()
{
TextView text;
text = (TextView) findViewById(R.id.textView);
text.setVisibility(View.INVISIBLE);
setContentView(text);
}
}
after getting it on my phone , when i touch any button : "Unfortunatly,app_test_1 has stopped !
can someone help me ?
Edit : i removed the setContentView and it works , thanks a lot guys. if someone has a good tuto that will help me to improve my android programming skills i'm a taker
Do not use setContentView() method outside of the onCreate()
just delete it in show and hide functions. Should work probably
Remove setContentView(text); and it will work. You don't need to call it.

Android TextView and Buttons not working in my app

I am learning Android and trying one simple Android app development, I got one demo code from my lecture and the teacher simply do the following:
There are 2 buttons, 1 textview. When touching button A, it will show "text A" in the textview, while touching button B, it will present "text B" in textview.
I followed the code and rewrote it, but i can't get the correct result when I ran with emulator.
When I touch either button, there's no content in the TextView. But my teacher's reference code works:
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
public class ActTwo extends Activity {
private TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
tv = (TextView) this.findViewById(R.id.textView2);
}
public void report(View v) {
if(v.getId() == R.id.button1)
tv.setText(R.string.anrep);
else
tv.setText(R.string.iprep);
}
}
How is report(View v) called? I can't understand how this class is called. Could someone please help me out?
You need to let your button know that, when pressed, report() should be called. This may be done through the android:onClick attribute of your button on your layout's XML:
<Button
android:id="#+id/button1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/button_text"
android:onClick="report" />
Or by code, attaching an OnClickListener to the button:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
button1 = (Button) this.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
report(v);
}
});
}
Hope it helps.
You should implements the View.OnClickListener, and override that OnClick method. You also need to setOnClickListener on your buttons. So your code should be like:
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
public class ActTwo extends Activity implements View.OnClickListener{
private TextView tv;
private Button button1, button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
tv = (TextView) this.findViewById(R.id.textView2);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.button1)
tv.setText(R.string.anrep);
else
tv.setText(R.string.iprep);
}
}
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
public class ActTwo extends Activity {
private TextView tv;
private Button button1, button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
tv = (TextView) this.findViewById(R.id.textView2);
button1 =(Button)this.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
report(v);
}
});
button2 =(Button)this.findViewById(R.id.button2):
button2.setOnClickListener(new OnClickListener(){
public void onClick(View v){
report(v);
}
});
}
public void report(View v) {
if(v.getId() == R.id.button1)
tv.setText(R.string.anrep);
else
tv.setText(R.string.iprep);
}
}

OnClickListener in Android, no errors but application stops

I have no idea why this code does not working. I have a TextView called text1 and Button called button1. I want to change the text in text1 after clicking button1.
I noticed that program works when I comment line button1.setOnClickListener(this); but consequently nothing happened.
package com.example.testowaniefragmentow;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements OnClickListener {
TextView text1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button_B1);
button1.setOnClickListener(this);
TextView text1 = (TextView) findViewById(R.id.textview_text);
if (savedInstanceState == null)
{
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,false);
return rootView;
}
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_B1:
text1.setText("Text has been changed");
break;
default:
break;
}
}
}
Change
setContentView(R.layout.activity_main);
to
setContentView(R.layout.fragment_main);
Hope it helps..
OR
as Mike said copy the whole contents of fragment_main.xml to activity_main.xml..
You have incorrectly defined your TextView. You have two text TextViews in your code. one is in your MainActivity class and the second is local variable for onCreate method. so in your onClicklistener when you try to set the text for text1 it is actually not been initialised. Please change your code in MainActivity class and onCreate method as following and keep everything as same.
//declare the view and button here.
TextView text1;
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialise the view and button here but do not declare it again or else they will become local variable
button1 = (Button) findViewById(R.id.button_B1);
button1.setOnClickListener(this);
text1 = (TextView) findViewById(R.id.textview_text);
if (savedInstanceState == null)
{
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
Try this bro:
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button_B1:
text1.setText("Text has been changed");
break;
default:
break;
}
}
});

Accessed from within inner Class

I am very new to Android programming and have a little problem.
The Error is:
Variable 'Demo_Button' is accessed from within inner class. Needs to declared final.
What i tried:
changed Demo_button.setImageResource(R.drawable.pressed); to final Demo_button.setImageResource(R.drawable.pressed);
package com.iklikla.eightgame;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ImageButton;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageButton Demo_button = (ImageButton)findViewById(R.id.imageButton);
Demo_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Demo_button.setImageResource(R.drawable.pressed);
}
});
}
}
A couple options here
First, I would declare it as a member variable then it will work
public class MainActivity extends Activity {
ImageButton Demo_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Demo_button = (ImageButton)findViewById(R.id.imageButton);
Second, since you are changing the View being clicked you can access it that way
emo_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ImageButton btn = (ImageButton)v; // cast the View to an ImageButton
btn.setImageResource(R.drawable.pressed);
}
});
Not related but will give you an error at runtime with the current code, you need to inflate a layout before trying to initialize that Button (most likely with setContentView()). So using my first example it would look something like
public class MainActivity extends Activity {
ImageButton Demo_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout); // where my_layout is the name of your layout
// file containing the Button without the xml extension
Demo_button = (ImageButton)findViewById(R.id.imageButton);

Unfortunately myfirstproject stopped working error in java

I know this is a very simple question for you experts,but please forgive me.Im very new to this android platform.
I tried to display a string in a textview when a button is clicked.
But it is not working.No errors are showing.
Can you please help me with where im going wrong.
Thanks in advace.
Below is the code i wrote:
package com.mycmpny.namebtn;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
public class NameonbuttonclickActivity extends Activity implements View.OnClickListener {
Button mybtn;
TextView txtView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mybtn= new Button(this);
mybtn.setOnClickListener(this);
printmyname();
setContentView(mybtn);
// setContentView(R.layout.main);
}
public void onClick(View view){
printmyname();
}
private void printmyname(){
txtView.setText("This is my first app");
}
}
I have done some modifications in onCreate().here i am making a textview which you left.Do that and say is it working ?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mybtn= new Button(this);
mybtn.setOnClickListener(this);
txtView=new TextView(this);
// printmyname();
LinearLayout ll=new LinearLayOut(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(mybtn);
ll.addView(txtView);
setContentView(ll);
// setContentView(R.layout.main);
}
HTH :)
You have not created a new object of TextView.
txtView = new TextView(this);
Else use findViewById if you've created the text View in your xml files.
txtView = (TextView)findViewById(R.id.TextView1);

Categories

Resources