user1366545
user1366545

Reputation: 13

Writing a reminder app unable to create a new object

I'm writing an app to create reminders. The app works like this-

Whenever the person enter's the reminder text and sets it.

A TextView displaying the reminder text, A CheckBox to enable/disable it, and a configure button to set the Reminder alarm should be created.

Instead of creating all of these objects seperately. I thought of making a Reminder Object. This object will be created whenever the user sets a new Reminder.

Here is my code. I don't know why the Reminder Object doesn't work.. and the app simply force closes.

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 (Can someone tell me what's wrong with this code? )

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

}

Thanks!

Upvotes: 0

Views: 171

Answers (1)

triggs
triggs

Reputation: 5900

Without a logcat this is a bit of a shot in the dark but

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

ReminderEditText is created here

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

but I don't see anywhere you call InitializeObjects, so you calling getText() on a null object because it hasn't been instantiated yet.

Upvotes: 1

Related Questions