将TextView添加到PopupWindow

时间:2013-12-07 04:27:51

标签: android textview popupwindow

private PopupWindow datepicker;
private void popupDatePicker() {
try {
    // We need to get the instance of the LayoutInflater
    LayoutInflater inflater = (LayoutInflater) Tabs.this
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.popup_datepicker,
    (ViewGroup) findViewById(R.id.popup_element));
    TextView valueTV;
    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
    //add LayoutParams
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    //add textView
    valueTV = new TextView(this);
    valueTV.setText("buy the ticket, take the ride");
    valueTV.setId(5);
    valueTV.setTextColor(Color.WHITE);
    valueTV.setLayoutParams(params);

    //add the textView to LinearLayout
    linearLayout.addView(valueTV);
    datepicker = new PopupWindow(layout, 300, 370, true);
    datepicker.setOutsideTouchable(true); 
    datepicker.setBackgroundDrawable(new BitmapDrawable()); 
    datepicker.setFocusable(true);
    datepicker.showAtLocation(layout, Gravity.CENTER, 0, 0);


} catch (Exception e) {
e.printStackTrace();
}
}

我在 linearLayout.addView(valueTV); 行收到错误。不确定有什么问题。弹出窗口甚至没有出现。我试过移动一些东西,但它仍然无法正常工作。

以下是记录问题的Log Cat:

12-07 00:46:54.984: W/System.err(882): java.lang.NullPointerException
12-07 00:46:55.044: W/System.err(882):  at com.package.Tabs.popupDatePicker(Tabs.java:503)
12-07 00:46:55.044: W/System.err(882):  at com.package.Tabs.access$8(Tabs.java:483)
12-07 00:46:55.044: W/System.err(882):  at com.package.less.Tabs$10.onClick(Tabs.java:307)
12-07 00:46:55.044: W/System.err(882):  at android.view.View.performClick(View.java:4424)
12-07 00:46:55.044: W/System.err(882):  at android.view.View$PerformClick.run(View.java:18383)
12-07 00:46:55.044: W/System.err(882):  at android.os.Handler.handleCallback(Handler.java:733)
12-07 00:46:55.044: W/System.err(882):  at android.os.Handler.dispatchMessage(Handler.java:95)
12-07 00:46:55.074: W/System.err(882):  at android.os.Looper.loop(Looper.java:137)
12-07 00:46:55.084: W/System.err(882):  at android.app.ActivityThread.main(ActivityThread.java:4998)
12-07 00:46:55.084: W/System.err(882):  at java.lang.reflect.Method.invokeNative(Native Method)
12-07 00:46:55.084: W/System.err(882):  at java.lang.reflect.Method.invoke(Method.java:515)
12-07 00:46:55.154: W/System.err(882):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-07 00:46:55.184: W/System.err(882):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-07 00:46:55.194: W/System.err(882):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

在弹出窗口中夸大布局时,您在访问LinearLayout时给出了错误的引用。改变如下:

View layout = inflater.inflate(R.layout.popup_datepicker,
(ViewGroup) findViewById(R.id.popup_element));
TextView valueTV;
LinearLayout linearLayout = (LinearLayout)layout.findViewById(R.id.linear_layout);

LinearLayout中,应将其初始化为(LinearLayout)layout.findViewById(R.id.linear_layout);