动态地向滑动抽屉添加内容会抛出NullPointerException

时间:2012-03-02 15:50:13

标签: android nullpointerexception slidingdrawer sliding

代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //    requestWindowFeature( Window.FEATURE_NO_TITLE );
    setContentView(R.layout.main);
    RelativeLayout d= (RelativeLayout)findViewById(R.id.drawer_content); 
    final LayoutInflater  inflate=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    TextView tv = (TextView)inflater.inflate(R.layout.text, null);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_TOP);

    tv.setLayoutParams(params);
    tv.setText("Appointments");
    Log.i("before", "add");
    d.addView(tv);
    Log.i("after", "add");
}

我一直试图在我的滑动抽屉中动态添加内容,但它似乎给了我NullPointerException

1 个答案:

答案 0 :(得分:0)

使用调试器并添加断点然后您可以按F6

逐行执行