0

私の国の地域 (judet) を選択し、テキストビューに設定するダイアログにボタンを追加しています。しかし、エラーが発生し、何が問題なのかわかりません。それが指しているコードは addView() ですが、ボタンには必要なすべての属性があるため、なぜ機能しないのかわかりません。

 judet = (RelativeLayout) findViewById(R.id.judet);


            final Dialog selectLocationDialog = new Dialog(this, R.style.DialogTheme);

            judet.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    final String [] judete = {"Alba","Arad","Arges","Bacau","Bihor","Bistrita-Nasaud","Botosani","Brasov","Braila","Buzau","Caras-Severin","Cluj","Constanta","Covasna","Dambovita","Dolj","Galati","Giurgiu","Gorj","Hargita","Hunedoara","Ialomita","Maramures","Mehedinti","Mures","Neamt","Olt","Prahova","Satu-Mare","Salaj","Sibiu","Teleorman","Timis","Tulcea","Valcea","Vaslui","Vrancea","Bucuresti","Ilfov","Calarasi","Iasi","Suceava"};
                    LayoutInflater inflater = HomeScreen.this.getLayoutInflater();

                    View selectRegion = inflater.inflate(R.layout.judet_oras_layout, null);
                    selectLocationDialog.setContentView(selectRegion);
                    WindowManager manager = (WindowManager) getSystemService(Activity.WINDOW_SERVICE);
                    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
                    lp.copyFrom(selectLocationDialog.getWindow().getAttributes());
                    lp.width = manager.getDefaultDisplay().getWidth();
                    lp.height = manager.getDefaultDisplay().getHeight() - manager.getDefaultDisplay().getHeight()/5;
                    selectLocationDialog.getWindow().setAttributes(lp);
                    final TextView judetTV = (TextView) findViewById(R.id.judetTV);

                    LinearLayout view = (LinearLayout) findViewById(R.id.judetOrasLayout);
                    int i=0;
                    while(judete[i] != null)
                    {
                        Button judetB = new Button(HomeScreen.this);
                        judetB.setText(judete[i]);
                        judetB.setTextColor(getResources().getColor(R.color.gray));
                        judetB.setBackgroundColor(getResources().getColor(R.color.grayLighter));
                        judetB.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                        final int aux = i;
                        judetB.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                // TODO Auto-generated method stub
                                judetTV.setText(judete[aux]);   
                                selectLocationDialog.dismiss();
                            }
                        });
                        view.addView(judetB);
                        i++;
                    }


                    selectLocationDialog.show();
                }
            });

彼女はアプリケーションのエラーログです:

08-27 07:25:35.971: E/AndroidRuntime(2639): FATAL EXCEPTION: main
08-27 07:25:35.971: E/AndroidRuntime(2639): java.lang.NullPointerException
08-27 07:25:35.971: E/AndroidRuntime(2639):     at com.xcommerce.androidstore.HomeScreen$28.onClick(HomeScreen.java:1502)
08-27 07:25:35.971: E/AndroidRuntime(2639):     at android.view.View.performClick(View.java:4202)
08-27 07:25:35.971: E/AndroidRuntime(2639):     at android.view.View$PerformClick.run(View.java:17340)
08-27 07:25:35.971: E/AndroidRuntime(2639):     at android.os.Handler.handleCallback(Handler.java:725)
08-27 07:25:35.971: E/AndroidRuntime(2639):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-27 07:25:35.971: E/AndroidRuntime(2639):     at android.os.Looper.loop(Looper.java:137)
08-27 07:25:35.971: E/AndroidRuntime(2639):     at android.app.ActivityThread.main(ActivityThread.java:5039)
08-27 07:25:35.971: E/AndroidRuntime(2639):     at java.lang.reflect.Method.invokeNative(Native Method)
08-27 07:25:35.971: E/AndroidRuntime(2639):     at java.lang.reflect.Method.invoke(Method.java:511)
08-27 07:25:35.971: E/AndroidRuntime(2639):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-27 07:25:35.971: E/AndroidRuntime(2639):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-27 07:25:35.971: E/AndroidRuntime(2639):     at dalvik.system.NativeStart.main(Native Method)
4

1 に答える 1

2

変化する

  final TextView judetTV = (TextView) findViewById(R.id.judetTV);

  LinearLayout view = (LinearLayout) findViewById(R.id.judetOrasLayout);

  final TextView judetTV = (TextView)selectLocationDialog.findViewById(R.id.judetTV);

  LinearLayout view = (LinearLayout)selectLocationDialog.findViewById(R.id.judetOrasLayout);
于 2013-08-27T07:43:46.707 に答える