0

I am trying to add a LinearLayout representing a little panel containing some elements to the main AbsoluteLayout of my Activity. I am using this code but the app crashes, this is the constructor of the panel class, that should add the view to the main layout. A similar code works to add runtime created buttons, but won't work for this.

public ButtonSetupPanel(String btnName, int keyCode, View v){
    AbsoluteLayout abs = (AbsoluteLayout) v.findViewById(R.id.mainbg);
    LinearLayout panel = (LinearLayout) v.findViewById(R.id.btnSetupPanel);
    panel.setLayoutParams(new AbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, 1023, 300));
    panel.setOnTouchListener(this);
    abs.addView(panel);
}

LogCat: (I am using StandOut library)

06-12 22:43:22.268: E/AndroidRuntime(8146): FATAL EXCEPTION: main 06-12 22:43:22.268: E/AndroidRuntime(8146): java.lang.RuntimeException: Unable to start service com.vektor.amapper.windows.MainWindow@413e44b0 with Intent { act=SHOW cmp=com.vektor.amapper/.windows.MainWindow (has extras) }: java.lang.NullPointerException 06-12 22:43:22.268: E/AndroidRuntime(8146): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2507) 06-12 22:43:22.268: E/AndroidRuntime(8146): at android.app.ActivityThread.access$1900(ActivityThread.java:130) 06-12 22:43:22.268: E/AndroidRuntime(8146): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292) 06-12 22:43:22.268: E/AndroidRuntime(8146): at android.os.Handler.dispatchMessage(Handler.java:99) 06-12 22:43:22.268: E/AndroidRuntime(8146): at android.os.Looper.loop(Looper.java:137) 06-12 22:43:22.268: E/AndroidRuntime(8146): at android.app.ActivityThread.main(ActivityThread.java:4745) 06-12 22:43:22.268: E/AndroidRuntime(8146): at java.lang.reflect.Method.invokeNative(Native Method) 06-12 22:43:22.268: E/AndroidRuntime(8146): at java.lang.reflect.Method.invoke(Method.java:511) 06-12 22:43:22.268: E/AndroidRuntime(8146): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 06-12 22:43:22.268: E/AndroidRuntime(8146): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 06-12 22:43:22.268: E/AndroidRuntime(8146): at dalvik.system.NativeStart.main(Native Method) 06-12 22:43:22.268: E/AndroidRuntime(8146): Caused by: java.lang.NullPointerException 06-12 22:43:22.268: E/AndroidRuntime(8146): at com.vektor.amapper.windows.ui.ButtonSetupPanel.(ButtonSetupPanel.java:18) 06-12 22:43:22.268: E/AndroidRuntime(8146): at com.vektor.amapper.windows.MainWindow.createAndAttachView(MainWindow.java:86) 06-12 22:43:22.268: E/AndroidRuntime(8146): at wei.mark.standout.ui.Window.(Window.java:150) 06-12 22:43:22.268: E/AndroidRuntime(8146): at wei.mark.standout.StandOutWindow.show(StandOutWindow.java:1079) 06-12 22:43:22.268: E/AndroidRuntime(8146): at wei.mark.standout.StandOutWindow.onStartCommand(StandOutWindow.java:382) 06-12 22:43:22.268: E/AndroidRuntime(8146): at com.vektor.amapper.windows.MainWindow.onStartCommand(MainWindow.java:150) 06-12 22:43:22.268: E/AndroidRuntime(8146): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2490) 06-12 22:43:22.268: E/AndroidRuntime(8146): ... 10 more

Update: I found out that the problem is that since this LinearLayout is in another XML file i need to inflate the other layout file and show the defined view (the LinearLayout). How to proceed?

Update2: I used this code to get the Needed View

public ButtonSetupPanel(String btnName, int keyCode, View v){
    AbsoluteLayout abs = (AbsoluteLayout) v.findViewById(R.id.mainbg);
    LinearLayout view = (LinearLayout) LayoutInflater.from(v.getContext()).inflate(R.layout.btnsetup , null);
    LinearLayout panel = (LinearLayout) view.findViewById(R.id.btnSetupPanel);
    view.removeView(panel);
    panel.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    panel.setX(512f);
    panel.setY(300f);
    panel.setOnTouchListener(this);
    abs.addView(panel);
}
Vektor88
  • 4,841
  • 11
  • 59
  • 111

1 Answers1

2

You know that AbsoluteLayout is deprecated since API level 3 ? Try to use RelativeLayout.

You can recreate absolute positioning by adding your child views to a RelativeLayout and set the RelativeLayout.LayoutParams to have only default values except width, height, marginTop and marginLeft. The top and left margin will be similar to top and left in AbsoluteLayout. Also, negative margins are supported.

Source: Alternative to AbsoluteLayout

Community
  • 1
  • 1
AndrewS
  • 7,418
  • 8
  • 35
  • 50
  • I am afraid that despite the Layout I am using there is some error in my code. – Vektor88 Jun 12 '13 at 15:06
  • 1
    my advise to you - use RelativeLayout - it can make the same things as Absolute and even better. ButtonSetupPanel.java:18 - what code in 18 line? – AndrewS Jun 12 '13 at 15:15
  • the setLayoutParams line. I'm afraid that the error is that the panel view is in a different xml file and this causes nullpointerexception – Vektor88 Jun 12 '13 at 15:17