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

时间:2013-04-11 10:27:19

标签: java android

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

每当此人输入提醒文本并进行设置时。

应创建显示提醒文本的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);
    }

}

谢谢!

1 个答案:

答案 0 :(得分: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()因为它尚未实例化。

相关问题