0

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

每当该人输入的提醒文本并设置它。

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

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

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

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);
    }

}

谢谢!

4

1 回答 1

1

如果没有 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,所以你调用getText()了一个空对象,因为它还没有被实例化。

于 2013-04-11T10:40:32.900 回答