Android的文本视图视图、文本、Android

2023-09-04 06:29:57 作者:感性不性感.

我创造的点击事件弹出窗口,里面的弹出窗口有文本视图和按钮...

如果我创建XML的ID和

设置文本

  TextView的TXT =(TextView中)findViewById(R.id.text)
txt.setText(信息);
 

我得到一个空指针异常。同样的情况对于一个按钮。

你真的了解AndroidNotification吗

下面是我的code

 包com.examples;

进口的java.util.ArrayList;
进口的java.util.Calendar;
进口java.util.GregorianCalendar中;
进口的java.util.List;
进口java.util.Locale中;
进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.graphics.Color;
进口android.os.Bundle;
进口android.text.format.DateFormat;
进口android.util.Log;
进口android.view.Gravity;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.view.View.OnClickListener;
进口android.widget.AdapterView;
进口android.widget.BaseAdapter;
进口android.widget.Button;
进口android.widget.GridView;
进口android.widget.ImageView;
进口android.widget.PopupWindow;
进口android.widget.TextView;
进口android.widget.Toast;
进口android.widget.AdapterView.OnItemClickListener;

公共类主要活动扩展实现OnClickListener
    {
    私人最终的String []月= {一月,二月,月,月,日,月,七五,八五,九五,十月,月, 十二月 };
        私有静态最后弦乐标签=主;
        私人按钮selectedDayMonthYearButton;
        私人按钮currentMonth;
        私人ImageView的prevMonth;
        私人ImageView的nextMonth;
        私人的GridView calendarView;
        私人GridCellAdapter适配器;
        私人日历_calendar;
        私人诠释月,年;
        私人最终日期格式dateFormatter =新的日期格式();
        私有静态最后弦乐dateTemplate =MMMM YYYY;
        私人PopupWindow PW;
        TextView的TXT1;
        静态ViewHolder持有人;
        私人无效getRequestParameters()
            {
                意向意图= getIntent();
                如果(意向!= NULL)
                    {
                        捆绑额外= intent.getExtras();
                        如果(临时演员!= NULL)
                            {
                                如果(临时演员!= NULL)
                                    {
                                        Log.d(标签,+++++ ----------------->+ extras.getString(PARAMS));
                                    }
                            }
                    }
            }


        / **第一次创建活动时调用。 * /
        @覆盖
        公共无效的onCreate(包savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                的setContentView(R.layout.simple_calendar_view);

                _calendar = Calendar.getInstance(Locale.getDefault());
                月= _calendar.get(Calendar.MONTH);
                Log.v(月,+月);
                年= _calendar.get(Calendar.YEAR);
                Log.v(年,+年);

                selectedDayMonthYearButton =(按钮)this.findViewById(R.id.selectedDayMonthYear);
                selectedDayMonthYearButton.setText(选择);

                prevMonth =(ImageView的)this.findViewById(R.id. prevMonth);
                prevMonth.setOnClickListener(本);

                currentMonth =(按钮)this.findViewById(R.id.currentMonth);
                currentMonth.setText(dateFormatter.format(dateTemplate,_calendar.getTime()));

                nextMonth =(ImageView的)this.findViewById(R.id.nextMonth);
                nextMonth.setOnClickListener(本);

                calendarView =(GridView控件)this.findViewById(R.id.calendar);

                //初始化
                适配器=新GridCellAdapter(getApplicationContext(),R.id.day_gridcell,月,年);
                adapter.notifyDataSetChanged();
                calendarView.setAdapter(适配器);
        }



        @覆盖
        公共无效的onClick(视图v)
            {
                如果(V = = prevMonth)
                    {
                        如果(月&其中; = 1)
                            {
                                月= 11;
                                年 - ;
                            } 其他
                            {
                                月 - ;
                            }

                        适配器=新GridCellAdapter(getApplicationContext(),R.id.day_gridcell,月,年);
                        _calendar.set(年,月,_calendar.get(Calendar.DAY_OF_MONTH));
                        currentMonth.setText(月[_calendar.get(Calendar.MONTH)] + - + _calendar.get(Calendar.YEAR));
                        。//currentMonth.setText(_calendar.getTime()的toString());

                        adapter.notifyDataSetChanged();
                        calendarView.setAdapter(适配器);
                    }
                如果(V == nextMonth)
                    {
                        如果(月> = 11)
                            {
                                一个月= 0;
                                今年++;
                            } 其他
                            {
                                一个月++;
                            }

                        适配器=新GridCellAdapter(getApplicationContext(),R.id.day_gridcell,月,年);
                        _calendar.set(年,月,_calendar.get(Calendar.DAY_OF_MONTH));
                        currentMonth.setText(月[_calendar.get(Calendar.MONTH)] + - + _calendar.get(Calendar.YEAR));
                        。//currentMonth.setText(_calendar.getTime()的toString());
                        adapter.notifyDataSetChanged();
                        calendarView.setAdapter(适配器);
                    }
            }

        //内部类
        公共类GridCellAdapter扩展了BaseAdapter实现OnClickListener
            {
                私有静态最后字符串变量=GridCellAdapter;
                私人最终语境_context;
                私人最终名单,其中,字符串>清单;
                私人最终的String []平日=新的String [] {太阳报,星期一,星期二,星期三,星期四,星期五,星期六};

                私人最终诠释[] daysOfMonth = {31,28,31,30,31,30,31,31,30,31,30,31};
                私人最终诠释月份,年份;
                INT daysInMonth,prevMonthDays;
                私人最终诠释currentDayOfMonth;
                私人按钮栅格单元;

                当月//天
                公共GridCellAdapter(上下文的背景下,INT textViewResourceId,INT月,年整型)
                    {
                        超();
                        this._context =背景;
                        this.list =新的ArrayList<字符串>();
                        this.month =月;
                        this.year =年;

                        Log.d(标签,月:+月++年+一年);
                        台历挂历= Calendar.getInstance();
                        currentDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);

                        printMonth(月,年);
                    }

                公共字符串的getItem(INT位置)
                    {
                        返回list.get(位置);
                    }

                @覆盖
                公众诠释getCount将()
                    {
                        返回则为list.size();
                    }

                私人无效printMonth(INT毫米,INT日)
                    {
                        //天数仍留空白
                        //这个月的开始。
                        INT trailingSpaces = 0;
                        INT leadSpaces = 0;
                        INT daysIn prevMonth = 0;
                        INT prevMonth = 0;
                        INT prevYear = 0;
                        INT nextMonth = 0;
                        INT nextYear = 0;

                        GregorianCalendar的CAL =新的GregorianCalendar(YY,MM,currentDayOfMonth);

                        当月//天
                        daysInMonth = daysOfMonth [毫米]
                        INT currentMonth =毫米;
                        如果(currentMonth == 11)
                            {
                                prevMonth = 10;
                                daysIn prevMonth = daysOfMonth [prevMonth]。
                                nextMonth = 0;
                                prevYear = YY;
                                nextYear = YY + 1;
                            }否则,如果(currentMonth == 0)
                            {
                                prevMonth = 11;
                                prevYear = YY  -  1;
                                nextYear = YY;
                                daysIn prevMonth = daysOfMonth [prevMonth]。
                                nextMonth = 1;
                            } 其他
                            {
                                prevMonth = currentMonth  -  1;
                                nextMonth = currentMonth + 1;
                                nextYear = YY;
                                prevYear = YY;
                                daysIn prevMonth = daysOfMonth [prevMonth]。
                            }

                        //计算出有多少的第一天之前才离开
                        // 月。
                        // getDay()返回0代表星期日。
                        trailingSpaces = cal.get(Calendar.DAY_OF_WEEK) -  1;

                        如果(cal.isLeapYear(cal.get(Calendar.YEAR))及&安培;毫米== 1)
                            {
                                ++ daysInMonth;
                            }

                        //尾随月天
                        的for(int i = 0; I< trailingSpaces;我++)
                            {
                                list.add(将String.valueOf((daysIn prevMonth  -  trailingSpaces + 1)+ I)+-grey+ - +月[prevMonth〕+ - + prevYear);
                            }

                        //当月天
                        的for(int i = 1; I< = daysInMonth;我++)
                            {
                                list.add(将String.valueOf(ⅰ)+-WHITE+ - +月[毫米] + - +日);
                            }

                        //领导每月的天数
                        的for(int i = 0; I<则为list.size()%7;我++)
                            {
                                Log.d(标签,下个月:=+个月[nextMonth]);
                                list.add(将String.valueOf第(i + 1)+-grey+ - +月[nextMonth] + - + nextYear);
                            }
                    }

                @覆盖
                众长getItemId(INT位置)
                    {
                        返回的位置;
                    }

                @覆盖
                公共查看getView(INT位置,查看convertView,父母的ViewGroup)
                    {
                        Log.d(标签,getView ......);
                        查看排= convertView;
                        如果(行== NULL)
                            {
                                // ROW通货膨胀
                                Log.d(标签,启动XML行通货膨胀......);
                                LayoutInflater充气=(LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                                行= inflater.inflate(R.layout.day_gridcell,父母,假);

                                Log.d(标签,成功完成XML行通货膨胀!);
                            }

                        //获取一个参考日栅格单元
                        栅格单元=(按钮)row.findViewById(R.id.day_gridcell);
                        gridcell.setOnClickListener(本);

                        //占间距

                        Log.d(标签,当前日期:+ currentDayOfMonth);
                        的String [] day_color = list.get(位置).split( - );
                        gridcell.setText(day_color [0]);
                        gridcell.setTag(day_color [0] + - + day_color [2] + - + day_color [3]);

                        如果(day_color [1] .equals(灰色))
                            {
                                gridcell.setTextColor(Color.LTGRAY);
                            }
                        如果(day_color [1] .equals(白))
                            {
                                gridcell.setTextColor(Color.WHITE);
                            }
                        如果(位置== currentDayOfMonth)
                            {
                                gridcell.setTextColor(Color.BLUE);
                            }

                        返回行;
                    }





                @覆盖
                公共无效的onClick(视图查看)
                    {

                    LayoutInflater充气=(LayoutInflater)Main.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                     //从相应的XML文件中夸大我们的观点
                     最后查看布局= inflater.inflate(R.layout.popup,(ViewGroup中)findViewById(R.id.popup_menu_root));

                    字符串date_month_year =(字符串)view.getTag();

                     PW =新PopupWindow(布局,200,75,真正的);
                     持有人=新ViewHolder();
                        holder.text1 =(TextView中)view.findViewById(R.id.pop_txt);
                        //holder.text1.setText("HIIIIIII);

                     pw.dismiss();
                     pw.showAtLocation(布局,Gravity.CENTER_VERTICAL,10,30);


                        Toast.makeText(getApplicationContext(),date_month_year,Toast.LENGTH_SHORT).show();
                        selectedDayMonthYearButton.setText(选择:+ date_month_year);
                        Log.v(按钮ID,+ R.id.bttn2);

                        //layout.setVisibility(View.INVISIBLE);

// findViewById(R.id.bttn2).setOnClickListener(
//新OnClickListener(){
////
//// // LayoutInflater吹气=(LayoutInflater)Main.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//// //夸大我们从相应的XML文件视图
//// //最后查看布局= inflater.inflate(R.layout.popup,(ViewGroup中)findViewById(R.id.popup_menu_root));
////
////
//// @覆盖
//公共无效的onClick(查看为arg0){
//// // TODO自动生成方法存根
////// PW =新PopupWindow(布局,200,75,真正的);
//////
////// pw.dismiss();
////// pw.showAtLocation(布局,Gravity.CENTER_VERTICAL,10,30);
////
//}
//});
                    }
            }



        静态类ViewHolder {
            TextView的文本1;

        }
        //list.setOnItemClickListener(new OnItemClickListener(){

    }
 

解决方案

正在调用 findViewById ()在查看得到了点击的,不是你的弹出窗口。

更改此:

  holder.text1 =(TextView中)view.findViewById(R.id.pop_txt);
 

要:

  holder.text1 =(TextView中)pw.findViewById(R.id.pop_txt);
 

I am creating a pop up window in click event, inside that pop up window there are text view and Button...

If I create an Id in XML and set the text by

TextView txt=(TextView)findViewById(R.id.text)
txt.setText("Message");

I get a Null pointer Exception. The same happens for a Button..

Here is my code

package com.examples;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Main extends Activity implements OnClickListener
    {
    private final String[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
        private static final String tag = "Main";
        private Button selectedDayMonthYearButton;
        private Button currentMonth;
        private ImageView prevMonth;
        private ImageView nextMonth;
        private GridView calendarView;
        private GridCellAdapter adapter;
        private Calendar _calendar;
        private int month, year;
        private final DateFormat dateFormatter = new DateFormat();
        private static final String dateTemplate = "MMMM yyyy";
        private PopupWindow pw;
        TextView txt1;
        static ViewHolder holder;
        private void getRequestParameters()
            {
                Intent intent = getIntent();
                if (intent != null)
                    {
                        Bundle extras = intent.getExtras();
                        if (extras != null)
                            { 
                                if (extras != null)
                                    {
                                        Log.d(tag, "+++++----------------->" + extras.getString("params"));
                                    }
                            }
                    }
            }


        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.simple_calendar_view);

                _calendar = Calendar.getInstance(Locale.getDefault());
                month = _calendar.get(Calendar.MONTH);
                Log.v("Months", ""+month);
                year = _calendar.get(Calendar.YEAR);
                Log.v("YEAR", ""+year);

                selectedDayMonthYearButton = (Button) this.findViewById(R.id.selectedDayMonthYear);
                selectedDayMonthYearButton.setText("Selected: ");

                prevMonth = (ImageView) this.findViewById(R.id.prevMonth);
                prevMonth.setOnClickListener(this);

                currentMonth = (Button) this.findViewById(R.id.currentMonth);
                currentMonth.setText(dateFormatter.format(dateTemplate, _calendar.getTime()));

                nextMonth = (ImageView) this.findViewById(R.id.nextMonth);
                nextMonth.setOnClickListener(this);

                calendarView = (GridView) this.findViewById(R.id.calendar);

                // Initialised
                adapter = new GridCellAdapter(getApplicationContext(), R.id.day_gridcell, month, year);
                adapter.notifyDataSetChanged();
                calendarView.setAdapter(adapter);
        }



        @Override
        public void onClick(View v)
            {
                if (v == prevMonth)
                    {
                        if (month <= 1)
                            {
                                month = 11;
                                year--;
                            } else
                            {
                                month--;
                            }

                        adapter = new GridCellAdapter(getApplicationContext(), R.id.day_gridcell, month, year);
                        _calendar.set(year, month, _calendar.get(Calendar.DAY_OF_MONTH));
                        currentMonth.setText(months[_calendar.get(Calendar.MONTH)] + "-" +_calendar.get(Calendar.YEAR));
                        //currentMonth.setText(_calendar.getTime().toString());

                        adapter.notifyDataSetChanged();
                        calendarView.setAdapter(adapter);
                    }
                if (v == nextMonth)
                    {
                        if (month >= 11)
                            {
                                month = 0;
                                year++;
                            } else
                            {
                                month++;
                            }

                        adapter = new GridCellAdapter(getApplicationContext(), R.id.day_gridcell, month, year);
                        _calendar.set(year, month, _calendar.get(Calendar.DAY_OF_MONTH));
                        currentMonth.setText(months[_calendar.get(Calendar.MONTH)] + "-" +_calendar.get(Calendar.YEAR));
                        //currentMonth.setText(_calendar.getTime().toString());
                        adapter.notifyDataSetChanged();
                        calendarView.setAdapter(adapter);
                    }
            }

        // Inner Class
        public class GridCellAdapter extends BaseAdapter implements OnClickListener
            {
                private static final String tag = "GridCellAdapter";
                private final Context _context;
                private final List<String> list;
                private final String[] weekdays = new String[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

                private final int[] daysOfMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
                private final int month, year;
                int daysInMonth, prevMonthDays;
                private final int currentDayOfMonth;
                private Button gridcell;

                // Days in Current Month
                public GridCellAdapter(Context context, int textViewResourceId, int month, int year)
                    {
                        super();
                        this._context = context;
                        this.list = new ArrayList<String>();
                        this.month = month;
                        this.year = year;

                        Log.d(tag, "Month: " + month + " " + "Year: " + year);
                        Calendar calendar = Calendar.getInstance();
                        currentDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);

                        printMonth(month, year);
                    }

                public String getItem(int position)
                    {
                        return list.get(position);
                    }

                @Override
                public int getCount()
                    {
                        return list.size();
                    }

                private void printMonth(int mm, int yy)
                    {
                        // The number of days to leave blank at
                        // the start of this month.
                        int trailingSpaces = 0;
                        int leadSpaces = 0;
                        int daysInPrevMonth = 0;
                        int prevMonth = 0;
                        int prevYear = 0;
                        int nextMonth = 0;
                        int nextYear = 0;

                        GregorianCalendar cal = new GregorianCalendar(yy, mm, currentDayOfMonth);

                        // Days in Current Month
                        daysInMonth = daysOfMonth[mm];
                        int currentMonth = mm;
                        if (currentMonth == 11)
                            {
                                prevMonth = 10;
                                daysInPrevMonth = daysOfMonth[prevMonth];
                                nextMonth = 0;
                                prevYear = yy;
                                nextYear = yy + 1;
                            } else if (currentMonth == 0)
                            {
                                prevMonth = 11;
                                prevYear = yy - 1;
                                nextYear = yy;
                                daysInPrevMonth = daysOfMonth[prevMonth];
                                nextMonth = 1;
                            } else
                            {
                                prevMonth = currentMonth - 1;
                                nextMonth = currentMonth + 1;
                                nextYear = yy;
                                prevYear = yy;
                                daysInPrevMonth = daysOfMonth[prevMonth];
                            }

                        // Compute how much to leave before before the first day of the
                        // month.
                        // getDay() returns 0 for Sunday.
                        trailingSpaces = cal.get(Calendar.DAY_OF_WEEK) - 1;

                        if (cal.isLeapYear(cal.get(Calendar.YEAR)) && mm == 1)
                            {
                                ++daysInMonth;
                            }

                        // Trailing Month days
                        for (int i = 0; i < trailingSpaces; i++)
                            {
                                list.add(String.valueOf((daysInPrevMonth - trailingSpaces + 1) + i) + "-GREY" + "-" + months[prevMonth] + "-" + prevYear);
                            }

                        // Current Month Days
                        for (int i = 1; i <= daysInMonth; i++)
                            {
                                list.add(String.valueOf(i) + "-WHITE" + "-" + months[mm] + "-" + yy);
                            }

                        // Leading Month days
                        for (int i = 0; i < list.size() % 7; i++)
                            {
                                Log.d(tag, "NEXT MONTH:= " + months[nextMonth]);
                                list.add(String.valueOf(i + 1) + "-GREY" + "-" + months[nextMonth] + "-" + nextYear);
                            }
                    }

                @Override
                public long getItemId(int position)
                    {
                        return position;
                    }

                @Override
                public View getView(int position, View convertView, ViewGroup parent)
                    {
                        Log.d(tag, "getView ...");
                        View row = convertView;
                        if (row == null)
                            {
                                // ROW INFLATION
                                Log.d(tag, "Starting XML Row Inflation ... ");
                                LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                                row = inflater.inflate(R.layout.day_gridcell, parent, false);

                                Log.d(tag, "Successfully completed XML Row Inflation!");
                            }

                        // Get a reference to the Day gridcell
                        gridcell = (Button) row.findViewById(R.id.day_gridcell);
                        gridcell.setOnClickListener(this);

                        // ACCOUNT FOR SPACING

                        Log.d(tag, "Current Day: " + currentDayOfMonth);
                        String[] day_color = list.get(position).split("-");
                        gridcell.setText(day_color[0]);
                        gridcell.setTag(day_color[0] + "-" + day_color[2] + "-" + day_color[3]);

                        if (day_color[1].equals("GREY"))
                            {
                                gridcell.setTextColor(Color.LTGRAY);
                            }
                        if (day_color[1].equals("WHITE"))
                            {
                                gridcell.setTextColor(Color.WHITE);
                            }
                        if (position == currentDayOfMonth)
                            {
                                gridcell.setTextColor(Color.BLUE);
                            }

                        return row;
                    }





                @Override
                public void onClick(View view)
                    {

                    LayoutInflater inflater = (LayoutInflater) Main.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                     // inflate our view from the corresponding XML file
                     final View layout = inflater.inflate(R.layout.popup, (ViewGroup)findViewById(R.id.popup_menu_root));

                    String date_month_year = (String) view.getTag();

                     pw = new PopupWindow(layout, 200,75, true);
                     holder = new ViewHolder();
                        holder.text1 = (TextView) view.findViewById(R.id.pop_txt);
                        //holder.text1.setText("HIIIIIII");

                     pw.dismiss();
                     pw.showAtLocation(layout, Gravity.CENTER_VERTICAL, 10, 30);


                        Toast.makeText(getApplicationContext(), date_month_year, Toast.LENGTH_SHORT).show();
                        selectedDayMonthYearButton.setText("Selected: " + date_month_year);
                        Log.v("Button ID",""+R.id.bttn2);

                        //layout.setVisibility(View.INVISIBLE);

//                      findViewById(R.id.bttn2).setOnClickListener(
//                              new OnClickListener() {
////                                    
////                                    //LayoutInflater inflater = (LayoutInflater) Main.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
////                                     // inflate our view from the corresponding XML file
////                                   //  final View layout = inflater.inflate(R.layout.popup, (ViewGroup)findViewById(R.id.popup_menu_root));
////                                    
////
////                                    @Override
//                                  public void onClick(View arg0) {
////                                        // TODO Auto-generated method stub
//////                                       pw = new PopupWindow(layout, 200,75, true);
//////                                              
//////                                       pw.dismiss();
//////                                       pw.showAtLocation(layout, Gravity.CENTER_VERTICAL, 10, 30);
////                                        
//                                  }
//                              });
                    }
            }



        static class ViewHolder {
            TextView text1;

        }
        //list.setOnItemClickListener(new OnItemClickListener(){

    }

解决方案

You are calling findViewById() on the View that got clicked, not your popup window.

Change this:

holder.text1 = (TextView) view.findViewById(R.id.pop_txt);

To:

holder.text1 = (TextView) pw.findViewById(R.id.pop_txt);