[英]Null Pointer Exception when using LayoutInflator()?
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. 我正在使用LayoutInflator
尝试使用户的文本显示在TableLayout
(这是scrollView
的子scrollView
),但是到目前为止,它只是一直抛出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? 所有ID都工作正常,我尝试清理项目以查看是否有帮助,但无济于事,有人可以帮助我吗?
By the way, it tells me the error is in 2 places, reminderListTextView.setText(text);
顺便说一句,它告诉我错误在2个地方, reminderListTextView.setText(text);
and in inflate("test");
和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);
}
}
It's actually in just one place that you get the exception : reminderListTextView.setText(text);
实际上,只有一个地方会出现异常: reminderListTextView.setText(text);
, the 2 lines are the trace to tell you that you get the error at reminderListTextView.setText(text);
,这两行是告诉您在reminderListTextView.setText(text);
处出现错误的跟踪reminderListTextView.setText(text);
when inflate("test");
当inflate("test");
is called. 叫做。
Try reminder.findViewById(...);
尝试reminder.findViewById(...);
, you need to use the view in which you inflated the layout to be able to retrieve elements from it. ,您需要使用在其中放大了布局的视图才能从布局中检索元素。
Try changing your inflate method to this: 尝试将您的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);
}
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 我不确定是否没有看到您的hinter_layout,但是我猜测ID为alerterListTextView的视图在您的hinter_layout.xml中,而不是在activity_main.xml中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.