使用LayoutInflater()时是否为空指针异常?

时间:2014-09-23 20:04:43

标签: java android layout-inflater

我正在使用LayoutInflator尝试让用户的文字显示在TableLayout(这是scrollView的孩子)但到目前为止,它只是一直在投掷空指针异常。所有的ID工作正常,我尝试清理项目,看看是否有帮助但无济于事,有人可以帮助我吗?

顺便说一下,它告诉我错误位于2个地方,reminderListTextView.setText(text);inflate("test");

代码:

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 个答案:

答案 0 :(得分:1)

实际上只有一个地方出现异常:reminderListTextView.setText(text);,这两行是告诉您reminderListTextView.setText(text);inflate("test");出错的追踪被称为。

尝试reminder.findViewById(...);,您需要使用夸大布局的视图才能从中检索元素。

答案 1 :(得分:1)

尝试将您的inflate方法更改为:

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

如果没有看到你的reminder_layout,我就不确定了,但是我猜你的提醒列表文件是在你的reminder_layout.xml中,而不是你的activity_main.xml