JAVA / ANDROID LayoutInflater

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • JAVA / ANDROID LayoutInflater

      Hallo allemaal,

      Momenteel zijn wij op school bezig met het programmeren van android apps, maar loop ik vast op de LayoutInflater. Op de één of andere manier vindt hij het .xml bestand dat ik wil toewijzen niet.

      Mijn code (waar het om gaat):

      PHP Source Code

      1. private void init() {
      2. LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      3. inflater.inflate(R.layout.rgbsliderview, this); //hier geeft hij aan: rgbsliderview cannot be resolved or is not a field
      4. r = (SeekBar) findViewById(R.id.seekBar1);
      5. g = (SeekBar) findViewById(R.id.seekBar2);
      6. b = (SeekBar) findViewById(R.id.seekBar3);
      7. SeekBarChangeListener listener = new SeekBarChangeListener();
      8. r.setOnSeekBarChangeListener(listener);
      9. g.setOnSeekBarChangeListener(listener);
      10. b.setOnSeekBarChangeListener(listener);
      11. model = ColorModel.getInstance();
      12. }
      Display All


      Heeft iemand enig idee waarom hij het xml bestand niet pakt? Deze is gewoon aangemaakt en zit in de /res/layout/ map.

      Edit: probleem is opgelost! De fout zat hem in dat ik import R.Java in het bestand zelf had staan. Echter loop ik nu tegen het volgende probleem aan:

      Wanneer ik nu custom view van de rgbseekbar wil toevoegen aan mijn activity_main geeft hij een java.lang.NullPointerException. Echter kan ik geen enkele insantie vinden die op null staat, terwijl dit niet zo zou moeten zijn. Volledige code voor de RGBSeekBar view :

      PHP Source Code

      1. package nl.rigault.colormix.view;
      2. import java.util.Observable;
      3. import java.util.Observer;
      4. import nl.rigault.colormix.R;
      5. import nl.rigault.colormix.model.ColorModel;
      6. import android.content.Context;
      7. import android.util.AttributeSet;
      8. import android.view.LayoutInflater;
      9. import android.widget.LinearLayout;
      10. import android.widget.SeekBar;
      11. public class RGBSeekBarView extends LinearLayout implements Observer {
      12. protected ColorModel model;
      13. private SeekBar r, g, b;
      14. public RGBSeekBarView(Context context) {
      15. super(context);
      16. init();
      17. }
      18. public RGBSeekBarView(Context context, AttributeSet attrs) {
      19. super(context, attrs);
      20. init();
      21. }
      22. public RGBSeekBarView(Context context, AttributeSet attrs, int defStyle) {
      23. super(context, attrs, defStyle);
      24. init();
      25. }
      26. private void init() {
      27. LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      28. inflater.inflate(R.layout.rgbseekbar, this);
      29. r = (SeekBar) findViewById(R.id.seekBar1);
      30. g = (SeekBar) findViewById(R.id.seekBar2);
      31. b = (SeekBar) findViewById(R.id.seekBar3);
      32. SeekBarChangeListener listener = new SeekBarChangeListener();
      33. r.setOnSeekBarChangeListener(listener);
      34. g.setOnSeekBarChangeListener(listener);
      35. b.setOnSeekBarChangeListener(listener);
      36. model = ColorModel.getInstance();
      37. model.addObserver(this);
      38. }
      39. @Override
      40. public void update(Observable observable, Object data) {
      41. r.setProgress(model.getR());
      42. g.setProgress(model.getG());
      43. b.setProgress(model.getB());
      44. }
      45. private class SeekBarChangeListener implements SeekBar.OnSeekBarChangeListener {
      46. @Override
      47. public void onProgressChanged(SeekBar seekBar, int progress,
      48. boolean fromUser) {
      49. if (!fromUser) {
      50. return;
      51. }
      52. if (seekBar == r) {
      53. model.setR(progress);
      54. }
      55. else if (seekBar == g) {
      56. model.setG(progress);
      57. }
      58. else {
      59. model.setB(progress);
      60. }
      61. }
      62. @Override
      63. public void onStartTrackingTouch(SeekBar seekBar) {
      64. // TODO Auto-generated method stub
      65. }
      66. @Override
      67. public void onStopTrackingTouch(SeekBar seekBar) {
      68. // TODO Auto-generated method stub
      69. }
      70. }
      71. }
      Display All

      The post was edited 2 times, last by thjeu ().

    • De error die hij geeft:

      PHP Source Code

      1. java.lang.NullPointerException
      2. at nl.rigault.colormix.view.RGBSeekBarView.init(RGBSeekBarView.java:47)
      3. at nl.rigault.colormix.view.RGBSeekBarView.<init>(RGBSeekBarView.java:27)
      4. at sun.reflect.GeneratedConstructorAccessor64.newInstance( at sun.reflect.DelegatingConstructorAccessorImpl.newInstance( at java.lang.reflect.Constructor.newInstance( at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instantiateClass(ProjectCallback.java:422)
      5. at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:179)
      6. at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
      7. at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:135)
      8. at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:755)
      9. at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
      10. at android.view.LayoutInflater.rInflate(LayoutInflater.java:727)
      11. at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
      12. at android.view.LayoutInflater.inflate(LayoutInflater.java:373)
      Display All


      Mijn codes:
      activity_main.xml:
      pastebin.com/SywKjPN0

      RGBColorView class :
      pastebin.com/uXuz1e54

      RGBSeekBarView class :
      pastebin.com/sBu4P5jp

      ColorModel class :
      pastebin.com/R9EZSrmS

      MainActivity class :
      pastebin.com/miKeTDaS

      package explorer :
      [Blocked Image: http://www.twistedmonkey.nl/package-explorer.png]

      EDIT:
      Als ik nu de seekbar wil toevoegen aan de activity_main.xml geeft hij deze error aan in de stacktrace :

      PHP Source Code

      1. java.lang.ClassCastException: com.android.layoutlib.bridge.MockView cannot be cast to android.view.ViewGroup
      2. at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:756)
      3. at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
      4. at android.view.LayoutInflater.rInflate(LayoutInflater.java:727)
      5. at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:758)
      6. at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
      7. at android.view.LayoutInflater.rInflate(LayoutInflater.java:727)
      8. at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
      9. at android.view.LayoutInflater.inflate(LayoutInflater.java:373)
      10. at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:399)
      11. at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:336)
      12. at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:334)
      13. at com.android.ide.eclipse.adt.internal.editors.layout.gle2.RenderService.createRenderSession(RenderService.java:451)
      14. at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1545)
      15. at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.recomputeLayout(GraphicalEditorPart.java:1302)
      16. at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.activated(GraphicalEditorPart.java:1059)
      17. at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditorDelegate.delegatePageChange(LayoutEditorDelegate.java:686)
      18. at com.android.ide.eclipse.adt.internal.editors.common.CommonXmlEditor.pageChange(CommonXmlEditor.java:360)
      19. at org.eclipse.ui.part.MultiPageEditorPart$2.widgetSelected(MultiPageEditorPart.java:292)
      20. at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
      21. at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
      22. at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
      23. at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1077)
      24. at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1062)
      25. at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:774)
      26. at org.eclipse.swt.custom.CTabFolder.setSelection(CTabFolder.java:3028)
      27. at org.eclipse.swt.custom.CTabFolder.onMouse(CTabFolder.java:1749)
      28. at org.eclipse.swt.custom.CTabFolder$1.handleEvent(CTabFolder.java:278)
      29. at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
      30. at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
      31. at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4169)
      32. at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3758)
      33. at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1053)
      34. at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
      35. at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:942)
      36. at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86)
      37. at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:588)
      38. at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
      39. at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:543)
      40. at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
      41. at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
      42. at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
      43. at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
      44. at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
      45. at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
      46. at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
      47. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      48. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      49. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      50. at java.lang.reflect.Method.invoke(Unknown Source)
      51. at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
      52. at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
      53. at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
      Display All


      De error visueel :
      [Blocked Image: http://www.twistedmonkey.nl/error-java.png]

      The post was edited 1 time, last by thjeu ().