0

I'm using the LayoutInflator to try and get the user's text to display in a TableLayout (Which is a child of a scrollView) But so far, it just keeps throwing the Null Pointer Exception. All the IDs are working fine, I tried cleaning the project to see if that would help but to no avail, can anyone help me out?

By the way, it tells me the error is in 2 places, reminderListTextView.setText(text); and in inflate("test");

Code:

public class MainActivity extends ActionBarActivity {

    private TableLayout reminderTableScrollView;

    // TextView and Button added
    EditText reminderEditText;

    Button addReminderButton;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        reminderTableScrollView = (TableLayout) findViewById(R.id.reminderTableScrollView);

        reminderEditText = (EditText) findViewById(R.id.reminderEditText);

        addReminderButton = (Button) findViewById(R.id.enterButton);
        addReminderButton.setOnClickListener(reminderClickListener);
    }

    private OnClickListener reminderClickListener = new Button.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (reminderEditText.getText().toString() == null || reminderEditText.getText().toString() == "") {
                Toast.makeText(getApplicationContext(), "Enter reminder", Toast.LENGTH_SHORT).show();
            } else {
                inflate("Test");
                reminderEditText.setText("");
            }
        }
    };

    public void inflate(String text) {
        LayoutInflater inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View reminder = inflator.inflate(R.layout.reminder_layout, null);

        TextView reminderListTextView = (TextView) findViewById(R.id.reminderListTextView);
        reminderListTextView.setText(text);

        reminderTableScrollView.addView(reminder);
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.menuAdd) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

2 Answers 2

1

It's actually in just one place that you get the exception : reminderListTextView.setText(text);, the 2 lines are the trace to tell you that you get the error at reminderListTextView.setText(text); when inflate("test"); is called.

Try reminder.findViewById(...);, you need to use the view in which you inflated the layout to be able to retrieve elements from it.

Sign up to request clarification or add additional context in comments.

Comments

1

Try changing your inflate method to this:

public void inflate(String text) {
    LayoutInflater inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View reminder = inflator.inflate(R.layout.reminder_layout, null);

    TextView reminderListTextView = (TextView)reminder.findViewById(R.id.reminderListTextView);
    reminderListTextView.setText(text);

    reminderTableScrollView.addView(reminder);
}

Without seeing your reminder_layout, I'm not certain, but I'm guessing the view with id reminderListTextView is in your reminder_layout.xml and not your activity_main.xml

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.