【问题标题】:Null Pointer Exception when using LayoutInflator()?使用 LayoutInflator() 时出现空指针异常?
【发布时间】:2014-09-24 04:10:53
【问题描述】:

我正在使用LayoutInflator 尝试让用户的文本显示在TableLayout 中(这是scrollView 的子代)但到目前为止,它只是不断抛出空指针异常。所有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);
    }
}

【问题讨论】:

    标签: java android layout-inflater


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      尝试将您的 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,我不确定,但我猜ID为remindListTextView的视图在你的reminder_layout.xml而不是你的activity_main.xml中

      【讨论】:

        猜你喜欢
        • 2013-07-19
        • 2020-03-21
        • 2014-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多