0

I want to open a popup window in my activity but I want to implement the popup from another class. I'm using this tutorial but my app crashes. I call the methods init() and popupInit() from my activity and everything else is here:

package com.example.victwo;

import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

public class PopupAudio implements OnClickListener {

    LinearLayout layoutOfPopup;
    PopupWindow popupMessage;
    Button popRecord, popStopRecord, popPlay, popStopPlaying;
    TextView popupText;

    public void popupInit() {
        popRecord.setOnClickListener(this);
        popStopRecord.setOnClickListener(this);
        popupMessage = new PopupWindow(layoutOfPopup, LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        popupMessage.setContentView(layoutOfPopup);
    }

    public void init(Context context) {
        popRecord = new Button(context);
        popRecord.setId(112);
        layoutOfPopup = new LinearLayout(context);
        popRecord.setText("REC");
        layoutOfPopup.setOrientation(1);
        layoutOfPopup.addView(popRecord);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()) {
        case 112:

            break;
        }
    }

}

This is what is given in the LogCat:

04-14 21:22:43.095: E/AndroidRuntime(575): FATAL EXCEPTION: main
04-14 21:22:43.095: E/AndroidRuntime(575): java.lang.NullPointerException
04-14 21:22:43.095: E/AndroidRuntime(575):  at com.example.victwo.PopupAudio.popupInit(PopupAudio.java:21)
04-14 21:22:43.095: E/AndroidRuntime(575):  at com.example.victwo.NoteActivity.onOptionsItemSelected(NoteActivity.java:172)
04-14 21:22:43.095: E/AndroidRuntime(575):  at android.app.Activity.onMenuItemSelected(Activity.java:2205)
04-14 21:22:43.095: E/AndroidRuntime(575):  at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:372)
04-14 21:22:43.095: E/AndroidRuntime(575):  at android.support.v7.app.ActionBarActivity.superOnMenuItemSelected(ActionBarActivity.java:244)
04-14 21:22:43.095: E/AndroidRuntime(575):  at android.support.v7.app.ActionBarActivityDelegateBase.onMenuItemSelected(ActionBarActivityDelegateBase.java:352)
04-14 21:22:43.095: E/AndroidRuntime(575):  at android.support.v7.app.ActionBarActivity.onMenuItemSelected(ActionBarActivity.java:130)
04-14 21:22:43.095: E/AndroidRuntime(575):  at android.support.v7.app.ActionBarActivityDelegateBase.onMenuItemSelected(ActionBarActivityDelegateBase.java:357)
04-14 21:22:43.095: E/AndroidRuntime(575):  at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:777)
04-14 21:22:43.095: E/AndroidRuntime(575):  at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
04-14 21:22:43.095: E/AndroidRuntime(575):  at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:922)
04-14 21:22:43.095: E/AndroidRuntime(575):  at android.support.v7.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:544)
04-14 21:22:43.095: E/AndroidRuntime(575):  at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:105)
04-14 21:22:43.095: E/AndroidRuntime(575):  at android.view.View.performClick(View.java:2485)
04-14 21:22:43.095: E/AndroidRuntime(575):  at android.view.View$PerformClick.run(View.java:9080)
04-14 21:22:43.095: E/AndroidRuntime(575):  at android.os.Handler.handleCallback(Handler.java:587)
04-14 21:22:43.095: E/AndroidRuntime(575):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-14 21:22:43.095: E/AndroidRuntime(575):  at android.os.Looper.loop(Looper.java:123)
04-14 21:22:43.095: E/AndroidRuntime(575):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-14 21:22:43.095: E/AndroidRuntime(575):  at java.lang.reflect.Method.invokeNative(Native Method)
04-14 21:22:43.095: E/AndroidRuntime(575):  at java.lang.reflect.Method.invoke(Method.java:507)
04-14 21:22:43.095: E/AndroidRuntime(575):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-14 21:22:43.095: E/AndroidRuntime(575):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-14 21:22:43.095: E/AndroidRuntime(575):  at dalvik.system.NativeStart.main(Native Method)

I don't know what is the problem and I'll be very grateful if you help me :)

frogatto
  • 28,539
  • 11
  • 83
  • 129
user3132352
  • 403
  • 1
  • 9
  • 24

1 Answers1

0

Check your code popStopRecord is null . You have not instantiated it

popStopRecord.setOnClickListener(this);

This is the line crash.

Libin
  • 16,967
  • 7
  • 61
  • 83
  • Yes, thank you, this was the problem. But now I have another. The popup window does not inflate. Can you help me, please? – user3132352 Apr 14 '14 at 18:34
  • check this http://mrbool.com/how-to-implement-popup-window-in-android/28285. If you still have problem , i would suggest to create new question – Libin Apr 14 '14 at 18:41