Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么当活动切换到这个屏幕时,它会变成一个黑屏?_Android - Fatal编程技术网

Android 为什么当活动切换到这个屏幕时,它会变成一个黑屏?

Android 为什么当活动切换到这个屏幕时,它会变成一个黑屏?,android,Android,} 我很困惑,它不是加载时间,因为我已经离开它一段时间了,它确实进行了交换活动,可以交换回原始的,而不是黑屏活动。有什么想法吗?尝试在super.onCreate()之后设置setContentView 编辑: 你还需要按照迈克的建议去做。也就是说,onCreate方法应该以小写的“O”开头。由于代码是当前的,因此不会调用onCreate方法,但是如果正确重命名该方法,仍然会出现黑屏,因为在调用super.onCreate()之后需要调用setContentView 我还为您浏览了其余的代码-您

}


我很困惑,它不是加载时间,因为我已经离开它一段时间了,它确实进行了交换活动,可以交换回原始的,而不是黑屏活动。有什么想法吗?

尝试在super.onCreate()之后设置setContentView

编辑:

你还需要按照迈克的建议去做。也就是说,onCreate方法应该以小写的“O”开头。由于代码是当前的,因此不会调用onCreate方法,但是如果正确重命名该方法,仍然会出现黑屏,因为在调用super.onCreate()之后需要调用setContentView

我还为您浏览了其余的代码-您真的应该将UI初始化代码移到onClick侦听器之外:

public class SecondScreenActivity extends Activity {
public void OnCreate(Bundle savedInstanceState){
    setContentView(R.layout.screen2);
    super.onCreate(savedInstanceState);


     Button button1 = (Button) findViewById(R.id.button1);

        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
            TextView counter;
            Intent i = getIntent();
             String gender = i.getStringExtra("gender");
             counter = (TextView) findViewById(R.id.textView23);
                int caloriesBurned = 0; int caloriesConsumed = 0;
                EditText consumedE;
                EditText burnedE;
                String test1, test2;
                test1 = getString(R.id.txtBurned);
                test2 = getString(R.id.txtConsumed);
                try {
                    if (test1 != "" && test2 != "") {
                        burnedE = (EditText) findViewById(R.id.txtBurned);
                        caloriesBurned = Integer.parseInt(burnedE.getText().toString().trim());
                        consumedE = (EditText) findViewById(R.id.txtConsumed);
                        caloriesConsumed = Integer.parseInt(consumedE.getText().toString().trim());

                    if(gender.contains("Male") && caloriesConsumed - caloriesBurned > 2000){
                        counter.setText("You are over your GDA of calories");
                    }
                    else{
                        counter.setText("You're a");
                    }
                    }
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
        });
}
这样,这些元素只需初始化一次,而不是每次单击按钮。它还使代码更易于阅读。

getIntent()解析为什么?
public class MyActivity extends Activity
{
    private TextView counter;
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen2);
        counter = (TextView) findViewById(R.id.myTextView);

        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() 
        {

        }
    }

}