gpt4 book ai didi

java - 编写提醒应用程序无法创建新对象

转载 作者:行者123 更新时间:2023-12-01 14:41:08 26 4
gpt4 key购买 nike

我正在编写一个应用程序来创建提醒。该应用程序的工作原理如下 -

每当用户输入提醒文本并进行设置时。

应该创建一个显示提醒文本的 TextView、一个用于启用/禁用它的复选框以及一个用于设置提醒警报的配置按钮。

而不是单独创建所有这些对象。我想到制作一个提醒对象。每当用户设置新的提醒时都会创建该对象。

这是我的代码。我不知道为什么提醒对象不起作用......并且应用程序只是强制关闭。

package com.aditya.patange.taskscheduler;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
EditText ReminderEditText;
int MAX_REMINDERS = 20;
int TEXT_SIZE = 20;
TextView[] textviews;
CheckBox[] checkboxes;
LinearLayout Llayout;
LayoutParams params;
SharedPreferences spref;
SharedPreferences.Editor sprefEditor;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Llayout = (LinearLayout) findViewById(R.id.linearlayout);
params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}



public void InitializeObjects() {
Llayout = (LinearLayout) findViewById(R.id.linearlayout);
ReminderEditText = (EditText) findViewById(R.id.addReminderText);
}


/*
* Toast Message
*/
public void ToastMessage(String Message) {
Toast.makeText(getApplicationContext(), Message, Toast.LENGTH_SHORT).show();
}


public String getReminderText() {
return ReminderEditText.getText().toString();
}

/*
* onClick method for the set Reminder Button
*/

public void setReminder_onClick(View v) {
/* Don't want a reminder with blank text..*/
if(!TextUtils.isEmpty(getReminderText())) {
ReminderWidget widget = new ReminderWidget(this);
widget.setText(getReminderText());
widget.setTextSize(20);
widget.setLayoutParams(params);
widget.displayReminderWidget(Llayout);
}
}

}

ReminderWidget.java(有人可以告诉我这段代码有什么问题吗?)

package com.aditya.patange.taskscheduler;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;



/*
* The Reminder Widget should consist of -
*
* [1] A TextView displaying the Reminder
* [2] A CheckBox with the Enable/Disable Toggle.
* [3] A configure Button
* (This should open a context Menu by which you can configure the Alarm)
* [4] ..lots to come
*
*/

public class ReminderWidget {

TextView textview;
CheckBox checkbox;
Button configButton;
SharedPreferences preference;
String ReminderText;
int ReminderTextSize;

public ReminderWidget(Context context) {
textview = new TextView(context);
checkbox = new CheckBox(context);
configButton = new Button(context);
configButton.setText("Configure");
}

public void displayReminderWidget(LinearLayout layout) {
layout.addView(textview);
layout.addView(checkbox);
layout.addView(configButton);
}

public void setText(String text) {
ReminderText=text;
textview.setText(ReminderText);
}

public String getText(String text) {
return ReminderText;
}

public void setTextSize(int size) {
ReminderTextSize = size;
textview.setTextSize(ReminderTextSize);
}

public int getTextSize() {
return ReminderTextSize;
}

public void setLayoutParams(LayoutParams params) {
textview.setLayoutParams(params);
checkbox.setLayoutParams(params);
configButton.setLayoutParams(params);
}

}

谢谢!

最佳答案

如果没有 logcat,这有点像黑暗中的尝试,但是

public String getReminderText() {
return ReminderEditText.getText().toString();
}

ReminderEditText 在此处创建

public void InitializeObjects() {
Llayout = (LinearLayout) findViewById(R.id.linearlayout);
ReminderEditText = (EditText) findViewById(R.id.addReminderText);
}

但我没有看到您在任何地方调用 InitializeObjects,因此您对 null 对象调用 getText(),因为它尚未实例化。

关于java - 编写提醒应用程序无法创建新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15946443/

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