gpt4 book ai didi

java - 尝试制作一个添加变量的简单应用程序,遇到一些问题

转载 作者:行者123 更新时间:2023-12-01 14:44:19 25 4
gpt4 key购买 nike

我有一个 24 小时格式的 timePicker,我想获取结果,将它们放入变量中,另外使用变量,然后将它们显示在 textView 中。 Eclipse 没有给我语法错误,但是当我运行该应用程序时,它在我启动“DisplayMessageActivity.java”时崩溃。

这是代码:

package com.example.sleepytime;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

@SuppressLint("NewApi")
public class DisplayMessageActivity extends Activity {

TimePicker timepicker;
public int hour1 = timepicker.getCurrentHour();
public int min1 = timepicker.getCurrentMinute();


@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);

timepicker = (TimePicker) findViewById(R.id.timePicker1);
timepicker.setIs24HourView(true);

Button mButton = (Button) findViewById(R.id.button1);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View printIt) {
final TextView mTextView = (TextView) findViewById(R.id.print);
mTextView.setText(hour1 + min1);
}

});

}

}

最佳答案

这里

public int hour1 = timepicker.getCurrentHour();
public int min1 = timepicker.getCurrentMinute();

时间选择器null。您需要将其放入 onCreate() 中,如下所示

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);

timepicker = (TimePicker) findViewById(R.id.timePicker1);
timepicker.setIs24HourView(true);
hour1 = timepicker.getCurrentHour();
min1 = timepicker.getCurrentMinute();

并改变

public int hour1 = timepicker.getCurrentHour();
public int min1 = timepicker.getCurrentMinute();

public int hour1;
public int min1;

这是我看到的第一个问题。可能还有更多,但这会让你无法继续前进。

 mButton.setOnClickListener(new View.OnClickListener() { 
public void onClick(View printIt) {
final TextView mTextView = (TextView) findViewById(R.id.print);
String hourString = String.valueOf(hour);
String minString = String.valueOf(minute);
mTextView.setText(hourString + minString);

关于java - 尝试制作一个添加变量的简单应用程序,遇到一些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15603255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com