动态添加视图时的NullPointerException(上下文?)

时间:2014-08-28 08:29:59

标签: android view android-context

我正在尝试添加和修改另一个布局内的布局中的一些文本。

我正在尝试通过调用下一个方法来执行此操作,但在尝试修改tv_event_hour的文本时崩溃了。

行:tv_event_hour.setText(getHour(timestamp) + " - ");

有什么问题?

public void addInputDescription(int timestamp, String description)
    {
        LinearLayout mRootLayout = (LinearLayout) findViewById(R.id.event_container);
        LayoutInflater inflater = (LayoutInflater) _c.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View mChildView = inflater.inflate(R.layout.program_event_day_header, mRootLayout, false);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mRootLayout.addView(mChildView, params);

        TextView tv_event_hour = (TextView) mChildView.findViewById(R.id.tv_event_hour);
        TextView tv_event_message = (TextView) mChildView.findViewById(R.id.tv_event_message);

        tv_event_hour.setText(getHour(timestamp) + " - ");
        tv_event_message.setText(description);
    }

public String getHour(long timeStamp){
    Date df = new Date(timeStamp*1000);
    return new SimpleDateFormat("HH:mm").format(df);
}

0 个答案:

没有答案