此代码不起作用,因为我添加了 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)