非活动类Android中的弹出窗口

时间:2014-04-14 18:13:13

标签: android class android-activity popup android-popupwindow

我想在我的活动中打开一个弹出窗口,但我想从另一个类实现弹出窗口。我正在使用此tutorial,但我的应用崩溃了。我从我的活动中调用方法init()和popupInit(),其他一切都在这里:

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;
        }
    }

}

这是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)

我不知道是什么问题,如果你帮助我,我将非常感激:)

1 个答案:

答案 0 :(得分:0)

检查您的代码popStopRecord是null。你没有实例化它

popStopRecord.setOnClickListener(this);

这是线路崩溃。