0

In an onCreate() method I have the following code:

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mapview);

    setupGui();
    mHandler = new Handler();

    mv = new MapView();
    (new Thread(){
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                    mHandler.post(new Runnable() {

                        @Override
                        public void run() {

                          mv.createPlane(true);
                        }
                    });
                } catch (Exception e) {
                }
            }
        }
    }).start();
}

Where mv is the name of the class I am in, which contains a createPlane(boolean) method for placing some new elements onto the UI. This method:

public void createPlane(boolean left) {
    linL = (LinearLayout) findViewById(R.id.ll);
}

And this does not work. (NullPointerException at findViewById())

Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
L.Bajczi
  • 9
  • 2
  • post the createPlane method, se we can help you – Vasileios Pallas Dec 07 '15 at 20:43
  • @vspallas After checking where exactly does the problem step up, this line was the one with the problem, so I see no reason to post the other several hundreds of lines. After this it stops working immediately. – L.Bajczi Dec 07 '15 at 20:46
  • @L.Bajczi please post the version of the code that worked. the shortest possible example. Otherwise, I can't help you any more than what I posted. – Alex K Dec 07 '15 at 20:47
  • It is either a problem with layout inflation (cant find a view in inflated layout) or you are modifying the UI from a thread other than UI thread, which is forbidden. In the second case, use runOnUIThread method for lines where you modify the UI to fix problems – Vladimír Gašpar Dec 07 '15 at 20:49
  • what are you trying to do to the linL? add a child or something else that needs the UiThread? – Vasileios Pallas Dec 07 '15 at 20:57

1 Answers1

0

Likely, the problem is with your order of things. You have to inflate the view before trying to find a view in that view by ID. You're trying to findViewById before you've inflated anything! All you have is the MapView object.

You need to ensure that before you run the thread, the view that contains R.id.ll is inflated.

Alex K
  • 8,269
  • 9
  • 39
  • 57
  • It's right after setContentView(R.layout.mapview); which does include this element. – L.Bajczi Dec 07 '15 at 20:48
  • @L.Bajczi before I continue to help, please add more code - a complete, short working set of code. OTherwise, it'll be a game of cat and mouse where I guess and you confirm or deny, and that doesn't work. – Alex K Dec 07 '15 at 20:49
  • I have edited the question, I hope this way it's understandable. Sorry. – L.Bajczi Dec 07 '15 at 20:54
  • @L.Bajczi yes - you are calling mv.makePlane() - mv is a specific instance of MapView. I don't think that it has been inflated yet. The problem may also be Vladimir's comment on your question. Explore that path as well. – Alex K Dec 07 '15 at 20:56