繁体   English   中英

使用LayoutInflator()时出现空指针异常?

[英]Null Pointer Exception when using LayoutInflator()?

我正在使用LayoutInflator尝试使用户的文本显示在TableLayout (这是scrollView的子scrollView ),但是到目前为止,它只是一直抛出Null Pointer Exception。 所有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);
    }
}

实际上,只有一个地方会出现异常: reminderListTextView.setText(text); ,这两行是告诉您在reminderListTextView.setText(text);处出现错误的跟踪reminderListTextView.setText(text); inflate("test"); 叫做。

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

尝试将您的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);
}

我不确定是否没有看到您的hinter_layout,但是我猜测ID为alerterListTextView的视图在您的hinter_layout.xml中,而不是在activity_main.xml中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM