这个android活动不起作用

时间:2013-07-23 23:15:48

标签: android

此代码无法正常工作,因为我添加了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)

3 个答案:

答案 0 :(得分:3)

更改您的代码:

将来自showPopup(this)的来电替换为:

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

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

修改1:

通常,100毫秒的延迟会在这种情况下产生奇迹。但是,即使在使用1秒延迟后看到你正在获得异常,我尝试了以下内容:

我创建了一个只包含LinearLayout的简单布局文件(例如,R.layout.simple_layout)。它的布局无关紧要。我给它一个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可能包含RelativeLayoutLinearLayout作为父布局。使用id为父级布局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);
        }  
    });
}

答案 1 :(得分:0)

我能看到的唯一奇怪的事情是,在为PopupWindow扩充布局时,你使用FrameLayout和R.id.popup作为父级。没有必要,我会传递null。

答案 2 :(得分:0)

onAttachedToWindow onDetachedFromWindow 是您的最佳选择。

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

<script>
$(document).ready(function () {
 var newNum = 1;
 cloneMe = function (el) {
     var newElem = el.clone().attr('id', 'author' + newNum);
     newElem.children().each(function (index, elem) {
         $(elem).attr('id', $(elem).attr('id') + newNum).attr('name', $(elem).attr('name') + newNum);
     });
     $('#cloneb').before(newElem);
     $('#delete_name' + newNum).html('<p id="rem_field"><a href="#"><span>Delete Line</span></a></p>');
     newNum++;
 };

 $('p#rem_field').live('click', function () {
     $(this).parents('div').remove();
     return false;
 });
</script>

<form name="submit" action="submit.php" method="GET">
    <div class="author" id="author" name="author">
        <div class="boxtitle" id="boxtitle" name="boxtitle" >
            <input  type="text" class="titleauthor" id="title" name="title">
            <p class="help-block" id="help-block" name="help-block">Title</p></div>
        <div class="box" id="box" name="box">
            <input  type="text" class="nameauthor" id="f_name" name="f_name">
            <p class="help-block" id="help-block" name="help-block">First name</p></div>
        <div class="box" id="box" name="box">
            <input type="text" class="nameauthor" id="l_name" name="l_name">
            <p class="help-block" id="help-block" name="help-block">Last name</p></div>
        <div class="box" id="box" name="box">
            <input type="text" class="nameauthor" id="email" name="email">
            <p class="help-block" id="help-block" name="help-block">E-mail address</p></div>
        <div class="checboxauthor" id="checkboxauthor" name="checboxauthor">
            <input type="checkbox" id="check" name="check">
           <p class="help-block" id="help-block" name="help-block">Contact</p></div>
        <div class="boxbutton" id="boxbutton" name="boxbutton">
        <button type="button" class="button" id="AuthorButton" onclick="cloneMe($('#author'));"/> Add author</button>
        </div>
    </div>
    <input class="submit" type="submit" name="submit"> 
</form>