-2

I have a button with the onClick name of checkResult.

public void checkResult(View view){
    LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    LinearLayout myRoot = new LinearLayout(this);
    View popupView = layoutInflater.inflate(R.layout.layout_result_popup, myRoot);
    final PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    //what I want to show in the popup
    TextView scorePopup = (TextView)findViewById(R.id.score_popup);
    scorePopup.setText("Your score: " + score);

    Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
    btnDismiss.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            popupWindow.dismiss();
        }});

    popupWindow.showAsDropDown(checkResultButton, 100, -1200);
    popupWindow.setFocusable(true);
    popupWindow.update();
}

When checkResult button is clicked, layout_result_popup is displayed.

I directly put a button at main activity and use onClick to show popup. So far, I managed to show the popup text in XML. But when I try to set some text to it, my app crashed.

Help please. :)

  • 2
    And what is in the logcat? – Aleks G Mar 14 '15 at 00:48
  • Thanks for the quick response. Here's what it says: Caused by: java.lang.NullPointerException – Ryan Phill Mar 14 '15 at 00:53
  • well, something, somewhere is null ... and you are trying to call `null.whatever(..)` or get `null.whatever` – Selvin Mar 14 '15 at 01:05
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Selvin Mar 14 '15 at 01:10
  • What is checkResultButton? – Fahim Mar 14 '15 at 01:45
  • checkResultButton is the button that when clicked, will show the popup. I've actually changed the name of some views / buttons here so that it seems less confusing to some people. – Ryan Phill Mar 14 '15 at 09:06

1 Answers1

0

Try to replace those 2 lines with this:

runOnUiThread(new Runnable() {
            @Override
            public void run() {
                TextView countView = (TextView)findViewById(R.id.count);
                countView .setText("Count: " + count+ "/" + totalCount);
            }
        });

It very likely even works with the first line outside of the runOnUiThread

Benyam Ephrem
  • 448
  • 6
  • 20
  • I tried this but it's not working. :( Logcat: Caused by: java.lang.reflect.InvocationTargetException – Ryan Phill Mar 14 '15 at 09:08
  • Check this out, it may help http://stackoverflow.com/questions/8958882/why-am-i-getting-an-invocationtargetexception-android-2d-game – Benyam Ephrem Mar 14 '15 at 10:20
  • @RyanPhill You are likely trying to operate on something that is `null` in your code. – Benyam Ephrem Mar 14 '15 at 10:27
  • @RyanPhill What is the second parameter of `inflate()` supposed to be? You put `null` for it – Benyam Ephrem Mar 14 '15 at 10:31
  • @RyanPhill I just checked the developer site for LayoutInflator and the second parameter is a `ViewGroup` for every type. That must be where you are getting the exception. You need to put something there. – Benyam Ephrem Mar 14 '15 at 10:35
  • Hi Benyam sorry for the late reply. I actually followed an online tutorial, that's why the second parameter of `inflate()` is `null`, since I did not change anything from the original code. Does this mean that the `null` is where I should make some changes at? – Ryan Phill Mar 17 '15 at 21:31
  • Well according to the documentation (http://developer.android.com/reference/android/view/LayoutInflater.html) you need to pass in a ViewGroup into the second parameter...passing in null would probably be the reason you are crashing. The ViewGroup would be a root view that is an "Optional view to be the parent of the generated hierarchy". Otherwise I can't immediately see why the app is crashing. Try that and if it doesn't work tell me. – Benyam Ephrem Mar 17 '15 at 22:01
  • So let's say MainActivity.java is the parent that holds this popup. A button called `popup` will show the popup. So this button is the one that I should insert into the parameter? – Ryan Phill Mar 18 '15 at 13:17
  • @RyanPhill Well...ViewGroup is the parent of the pop-up you would put in. It wouldn't be a button since that isn't a viewgroup. Read this ( http://developer.android.com/reference/android/view/ViewGroup.html). Whatever view the pop up is generating from is the parent view – Benyam Ephrem Mar 18 '15 at 14:34
  • @RyanPhill Also post your log where the error occurs. So I can see the errors and execptions you are getting. – Benyam Ephrem Mar 18 '15 at 14:43
  • Thanks for being so helpful all the way, Benyam. I still couldn't get it working. Not sure why. I have edited my question, so that you can see my actual code. Please have a look. – Ryan Phill Mar 18 '15 at 19:37
  • @RyanPhill Post the log of the error you get in the question so we can know exactly what lines are causeing the crash and what the errors are. Another thing I see is one thing you should do is declare your varibales within the button outside instead. The button shouldn't be where a `TextView` is identified. The code within a button should just tell those things what to do. – Benyam Ephrem Mar 18 '15 at 20:55
  • This is what I got from logcat: `Caused by: java.lang.reflect.InvocationTargetException`. Sorry that I forgot to include this. The reason why I didn't declare my text view outside is because at `onCreate`, the xml layout is `setContentView(R.layout.activity_quiz);`, but the popup xml is `R.layout.layout_result_popup`. – Ryan Phill Mar 19 '15 at 14:21
  • Edit your question with the whole log – Benyam Ephrem Mar 19 '15 at 14:25
  • What are the _exact_ line(s) throwing the exception, a `InvocationTargetException` wraps an exception so it is likely a `NullPointerException` being wrapped, edit the question with the whole logcat. It tells you what line is causing it so locate the line and edit the question to show it – Benyam Ephrem Mar 19 '15 at 14:30
  • @RyanPhill It doesn't matter what you set the content view as...you can declare all those view variables in onCreate...you aren't setting the content view to the pop-up's XML – Benyam Ephrem Mar 19 '15 at 14:50
  • Hi Benyam sorry for the late reply. I was abroad for the past week. Would you mind if we discuss about this via email? My email address is ryanphill02@gmail.com – Ryan Phill Mar 26 '15 at 16:33
  • Thanks a lot Benyam. Everything works fine now. So glad to have had your help! – Ryan Phill Apr 13 '15 at 19:44