Android can't add button to dialog ( layout of the dialog not a dialog
button itself )
I'm adding buttons to a dialog that selects an area in my country (judet)
and set's it to a textview. But I'm getting errors and cant figure out
what's wrong from them. The code it's pointing to is addView() but I have
no idea why it shouldn't work since the button has all the attributes it
needs.
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();
}
});
Her is the error log of the application:
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)
No comments:
Post a Comment