나는를 사용하여 LayoutInflator
사용자의 텍스트를 TableLayout
(A의 자식)에 표시하려고 시도하고 scrollView
있지만 지금까지는 Null 포인터 예외가 계속 발생합니다. 모든 ID가 잘 작동하고 있습니다. 프로젝트를 정리하여 도움이되는지 확인했지만 아무 소용이 없었습니다. 누구든지 나를 도울 수 있습니까?
그건 그렇고, 오류가 두 곳에서 발생 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);
될 때 오류가 발생했음을 알려주는 추적 inflate("test");
입니다.
시도해보십시오 reminder.findViewById(...);
. 레이아웃을 확장 한 뷰를 사용하여 요소를 검색 할 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다