3

此代码不起作用,因为我添加了 showPopup() 方法,我无法弄清楚原因。这段代码有什么问题?

package com.example.healthcity.activities;

import org.taptwo.android.widget.CircleFlowIndicator;
import org.taptwo.android.widget.ViewFlow;

import com.example.healthcity.R;
import com.example.healthcity.dataobjs.DemoDataAdapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

public class DemoViewFlowActivity extends Activity {

    private ViewFlow viewFlow;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bf_home_screen);
        // viewFlow = (ViewFlow) findViewById(R.id.viewflow);
        // viewFlow.setAdapter(new DemoDataAdapter(this));
        // CircleFlowIndicator indic = (CircleFlowIndicator)
        // findViewById(R.id.viewflowindic);
        // viewFlow.setFlowIndicator(indic);
    }

    @Override
    protected void onStart() {
        super.onStart();
        showPopup(DemoViewFlowActivity.this);
    }

    // The method that displays the popup.
    private void showPopup(Activity context) {
        int popupWidth = 200;
        int popupHeight = 150;

        // Inflate the popup_layout.xml
        FrameLayout viewGroup = (FrameLayout) context.findViewById(R.id.popup);
        LayoutInflater layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = layoutInflater.inflate(R.layout.circle_layout, viewGroup);

        // Creating the PopupWindow
        PopupWindow popup = new PopupWindow(context);
        popup.setContentView(layout);
        popup.setWidth(popupWidth);
        popup.setHeight(popupHeight);
        popup.setFocusable(true);

        // Some offset to align the popup a bit to the right, and a bit down,
        // relative to button's position.
        int OFFSET_X = 30;
        int OFFSET_Y = 30;

        // Displaying the popup at the specified location, + offsets.
        popup.showAtLocation(layout, Gravity.NO_GRAVITY, 50, 50);

         viewFlow = (ViewFlow) findViewById(R.id.viewflow);
         viewFlow.setAdapter(new DemoDataAdapter(this));
         CircleFlowIndicator indic = (CircleFlowIndicator)
         findViewById(R.id.viewflowindic);
         viewFlow.setFlowIndicator(indic);

        // Getting a reference to Close button, and close the popup when
        // clicked.
        // Button close = (Button) layout.findViewById(R.id.close);
        // close.setOnClickListener(new OnClickListener() {
        //
        // @Override
        // public void onClick(View v) {
        // popup.dismiss();
        // }
        // });
    }
}

这是 logcat 的一部分,其中似乎出现了错误:

    07-23 23:34:25.960: W/dalvikvm(4048): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-23 23:34:25.969: E/AndroidRuntime(4048): FATAL EXCEPTION: main
07-23 23:34:25.969: E/AndroidRuntime(4048): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.healthcity/com.example.healthcity.activities.DemoViewFlowActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
07-23 23:34:25.969: E/AndroidRuntime(4048):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-23 23:34:25.969: E/AndroidRuntime(4048):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-23 23:34:25.969: E/AndroidRuntime(4048):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-23 23:34:25.969: E/AndroidRuntime(4048):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-23 23:34:25.969: E/AndroidRuntime(4048):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 23:34:25.969: E/AndroidRuntime(4048):     at android.os.Looper.loop(Looper.java:137)
07-23 23:34:25.969: E/AndroidRuntime(4048):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-23 23:34:25.969: E/AndroidRuntime(4048):     at java.lang.reflect.Method.invokeNative(Native Method)
07-23 23:34:25.969: E/AndroidRuntime(4048):     at java.lang.reflect.Method.invoke(Method.java:511)
07-23 23:34:25.969: E/AndroidRuntime(4048):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-23 23:34:25.969: E/AndroidRuntime(4048):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-23 23:34:25.969: E/AndroidRuntime(4048):     at dalvik.system.NativeStart.main(Native Method)
07-23 23:34:25.969: E/AndroidRuntime(4048): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
07-23 23:34:25.969: E/AndroidRuntime(4048):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:567)
07-23 23:34:25.969: E/AndroidRuntime(4048):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
07-23 23:34:25.969: E/AndroidRuntime(4048):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
07-23 23:34:25.969: E/AndroidRuntime(4048):     at android.widget.PopupWindow.invokePopup(PopupWindow.java:993)
07-23 23:34:25.969: E/AndroidRuntime(4048):     at android.widget.PopupWindow.showAtLocation(PopupWindow.java:847)
07-23 23:34:25.969: E/AndroidRuntime(4048):     at android.widget.PopupWindow.showAtLocation(PopupWindow.java:811)
07-23 23:34:25.969: E/AndroidRuntime(4048):     at com.example.healthcity.activities.DemoViewFlowActivity.showPopup(DemoViewFlowActivity.java:81)
07-23 23:34:25.969: E/AndroidRuntime(4048):     at com.example.healthcity.activities.DemoViewFlowActivity.onStart(DemoViewFlowActivity.java:54)
07-23 23:34:25.969: E/AndroidRuntime(4048):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
07-23 23:34:25.969: E/AndroidRuntime(4048):     at android.app.Activity.performStart(Activity.java:5114)
07-23 23:34:25.969: E/AndroidRuntime(4048):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
4

3 回答 3

3

像这样更改您的代码:

将调用替换为showPopup(this)

new Handler().postDelayed(new Runnable() {

    public void run() {
        showPopup(DemoViewFlowActivity.this);
    }
}, 100L);

编辑1:

通常,在这种情况下,100 毫秒的延迟会产生奇迹。但是,看到即使在使用 1 秒延迟后您也遇到了异常,我尝试了以下操作:

我制作了一个简单的布局文件(比如 R.layout.simple_layout),其中仅包含一个LinearLayout. 它是什么布局并不重要。我给了它一个id(比如,@+id/llEmpty)。现在,在我的 onCreate(Bundle) 中,我使用setContentView(R.layout.simple_layout). 而且,在我的 中onStart(),我输入了以下内容:

@Override
protected void onStart() {      
    super.onStart();

    findViewById(R.id.llEmpty).post(new Runnable() {

        @Override
        public void run() {
            // method that creates and displays the popup
            showList();
        }  
    });
}

这将如何转化为您的代码:

您的布局文件R.layout.bf_home_screen可能包含 aRelativeLayout或 aLinearLayout作为父布局。给父布局一个idusing android:id="@+id/some_id"。将您的方法更改onStart()为:

@Override
protected void onStart() {      
    super.onStart();

    // Replace "some_id" with the id you use in the layout file
    findViewById(R.id.some_id).post(new Runnable() {

        @Override
        public void run() {
            showPopup(DemoViewFlowActivity.this);
        }  
    });
}
于 2013-07-23T23:23:22.520 回答
0

我能看到的唯一奇怪的事情是,在为 PopupWindow 扩展布局时,您正在使用带有 R.id.popup 作为父级的 FrameLayout。没有必要,我会通过null。

于 2013-07-23T23:24:21.923 回答
0

onAttachedToWindowonDetachedFromWindow是您的最佳选择。

并且不需要 postDelayed(Runnable, 100)。因为这 100 毫不保证任何东西

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    Log.d(TAG, "onAttachedToWindow");

    showPopup(DemoViewFlowActivity.this);
}

@Override
public void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    Log.d(TAG, "onDetachedFromWindow");

    popup.dismiss();
}
于 2015-11-19T16:51:58.177 回答