2014-09-23 94 views
0

我使用LayoutInflator來試圖讓用戶的文本顯示在TableLayout(這是scrollView的孩子)但是到目前爲止,它只是一直拋出空指針異常。所有的ID工作正常,我試圖清理項目,看看是否會有所幫助,但無濟於事,任何人都可以幫助我?使用LayoutInflator()時出現空指針異常?

順便說一句,它告訴我的錯誤是在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); 
    } 
} 

回答

1

它實際上是在一個地方,你得到的異常:reminderListTextView.setText(text);,該2行是跡線告訴你,當調用inflate("test");時,你在reminderListTextView.setText(text);處得到錯誤。

嘗試reminder.findViewById(...);,您需要使用填充佈局的視圖,以便能夠從其中檢索元素。

1

試着改變你的充氣方法是:

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 reminderListTextView是在你的reminder_layout.xml,而不是你的activity_main .xml