我的第一个 android 应用出现问题

issue with my first android app

在 post 提出我的问题之前,我想澄清一下这是我在 Whosebug 上的第一个 post 让我们开始讲故事吧。

正如标题所说,我正在 android 上制作我的第一个应用程序,我发现自己遇到了问题。

我的应用程序上有 3 个按钮: button1 : 给 textview2 "hello world again " 并使其可见 // button2 : 使 textView2 不可见 // button3 : 使 textView1 不可见

这是代码 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);
}
}

在我的 phone 上获得它后,当我触摸任何按钮时:“不幸的是,app_test_1 已停止! 有人可以帮我吗?

编辑:我删除了 setContentView 并且它有效,非常感谢大家。如果有人有好的 tuto 可以帮助我提高我的 android 编程技能,我是一个接受者

不要在 onCreate() 之外使用 setContentView() 方法 只需在显示和隐藏功能中将其删除即可。应该可以工作

移除setContentView(文本);它会起作用的。你不需要调用它。