简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM