5

EditTextで長いクリックをしようとしていますが、長いクリックをすると以下のエラーが表示されます。長いクリックでコピー/ペースト/すべて選択コンテ​​キストポップアップを取得し、テキストをボックスに貼り付けることができるようにしたいと考えています。長押しのEditTextクラッシュ:ウィンドウを追加できません

Fatal Exception: android.view.WindowManager$BadTokenException: Unable to add window -- token [email protected] is not valid; is your activity running? 

EditTextはPopupWindowのScrollViewにあります。だから、エラーが発生すると、私は現在、PopupWindowを開いた状態でアクティビティをアクティブにしています.PopupWindowに含まれているEditText内でLong Clickを実行します。 EditTextを含む

Gradleの設定

compileSdkVersion 25 
buildToolsVersion '25.0.0' 
defaultConfig { 
    applicationId 'com.accoservice.cico' 
    minSdkVersion 17 
    targetSdkVersion 25 
    versionCode 37 
    versionName '4.2.6' 
    multiDexEnabled true 
} 

レイアウト:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/outer_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#73000000"> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:layout_marginTop="100dp" 
     android:layout_marginBottom="5dp" 
     android:background="#ffffff" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/title" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="5dp" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="15dp" 
      android:singleLine="true" 
      android:text="@string/note_msg" 
      android:textColor="#62CCFE" 
      android:textSize="18sp" /> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="2dp" 
      android:layout_marginBottom="5dp" 
      android:layout_marginTop="10dp" 
      android:background="#62CCFE" /> 

     <ScrollView 
      android:id="@+id/sv_resolution_note" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp" 
      android:layout_marginTop="5dp"> 

      <EditText 
       android:id="@+id/et_note_msz" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_above="@+id/view" 
       android:layout_alignParentTop="true" 
       android:scrollbars="vertical" 
       android:focusable="true" 
       android:gravity="left" 
       android:maxLines="20" 
       android:hint="@string/write_note" 
       android:inputType="textFilter|textMultiLine|textCapSentences" 
       android:singleLine="false" 
       android:textIsSelectable="true" 
       android:enabled="true" 
       android:longClickable="true" /> 
     </ScrollView> 

     <View 
      android:id="@+id/view" 
      android:layout_width="fill_parent" 
      android:layout_height="1dp" 
      android:layout_above="@+id/send_note" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:background="@android:color/darker_gray" /> 

     <Button 
      android:id="@+id/send_note" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_above="@+id/viewss" 
      android:layout_gravity="center" 
      android:background="@color/white" 
      android:text="@string/add_note" /> 

     <View 
      android:id="@+id/viewss" 
      android:layout_width="fill_parent" 
      android:layout_height="1dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 

      android:background="@android:color/darker_gray" /> 

    </LinearLayout> 

</LinearLayout> 

ポップアップウィンドウ:

@Override 
public void onClick(View v) { 
    noteDialog(getResources().getString(R.string.laborentryresolutionstart), tv_labor_entry_resolution_start); 
} 

public void noteDialog(String noteTitle, final TextView tv_resolution_note) 
{ 
    LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); 
      final View popupView; 
      popupView = layoutInflater.inflate(R.layout.resolution_note, null); 

      TextView title = (TextView) popupView.findViewById(R.id.title); 
      title.setText(noteTitle); 

      final EditText editText = (EditText) popupView.findViewById(R.id.et_note_msz); 
      final PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); 
      popupWindow.update(); 
      popupWindow.setFocusable(true); 
      popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
      editText.setEnabled(false); 
      editText.setEnabled(true); 
      editText.setFocusable(true); 
      editText.setOnLongClickListener(new View.OnLongClickListener() { 
       @Override 
       public boolean onLongClick(View v) { 

        //ADD HERE ABOUT CUT COPY PASTE 
        // TODO Auto-generated method stub 
        return false; 
       } 
      }); 

      if (!tv_resolution_note.getText().toString().isEmpty()) { 
       editText.setText(tv_resolution_note.getText().toString()); 
      } 

      Button btnDone = (Button) popupView.findViewById(R.id.send_note); 
      LinearLayout outer_layout = (LinearLayout) popupView.findViewById(R.id.outer_layout); 
      outer_layout.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        popupWindow.dismiss(); 

        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
        imm.toggleSoftInput(InputMethodManager.RESULT_HIDDEN, 0); 
       } 
      }); 

      System.gc(); 
      try { 
       btnDone.setOnClickListener(new Button.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         EditText noteMsz = (EditText) popupView.findViewById(R.id.et_note_msz); 
         tv_resolution_note.setText(noteMsz.getText().toString()); 

         popupWindow.dismiss(); 

         InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
         imm.toggleSoftInput(InputMethodManager.RESULT_HIDDEN, 0); 

         invalidateOptionsMenu(); 
        } 
       }); 
      } catch (Exception e) { 
      } 

      popupWindow.setFocusable(true); 
      popupWindow.setBackgroundDrawable(new BitmapDrawable(null, "")); 
      popupWindow.showAsDropDown(tv_labor_sym_entry, 0, -60); 
      popupWindow.update();  
} 
+0

アクティビティでこのコードを呼び出していますか?サービス?あなたがアクティビティであれば画面から外れていないと確信していますか?このメッセージは、通常、サービスからUIを起動しようとしたとき、またはアクティビティが完了した後でダイアログをポップアップしようとしたときに発生します。 –

+0

アクティビティ中にポップアップウィンドウのコードを呼び出しています。アクティビティが現在実行中で、ポップアップウィンドウがアクティブで、EditTextで長押ししてエラーを受け取ります。 – Adam

+1

API 24を実行しているエミュレータで少し修正してコードを実行しましたが、問題なく動作します。つまり、クラッシュしておらず、長く押すリスナーが期待通りに呼び出されるということです。物事をどのように設定しているかについて、さらに詳しい情報を提供できますか?あなたがテストしているAPI? [ここ](https://gist.github.com/Cheticamp/08ebef491a727a12577a7e9790eaa752)は、私があなたに役立つ場合に使用した活動の要点です。 – Cheticamp

答えて

0

で私たちは、ポップアップを奪うとがあったバグを解消するために再設計を行ってきました。

0

あなたのポップアップを早すぎて呼び出すかもしれません。私の例(gist here)では、ボタン・プレスからポップアップ・コードを実行しています。これはonCreate()の後にあり、他のキーライフサイクルメソッドが実行されます。この例では、すべて正常に動作します。

しかし、onCreate()でポップアップをインスタンス化しようとすると、「android.view.WindowManager $ BadTokenException:あなたが見ているウィンドウを追加できません」というログカットエラーが発生します。

あなたのポップアップをすぐにインスタンス化しようとしていると思います。アクティビティのライフサイクルの後半で、そしてonCreate()が実行された後で確実に開始してください。それをインスタンス化する必要がある場合は、post(Runnable)への呼び出しを介してUIメッセージキューにコードを添付することができます。 (here参照)。

私はこれがあなたの問題であることをかなり確信しています。これで問題が解決しない場合は、ポップアップウィンドウをどのようにインスタンス化するかについての詳細を質問に更新してください。

+0

質問にもっとコードを追加して、何が起こっているのかをもっと見ることができます。 – Adam

+0

@ Adamあなたが提供したもので問題を再現することはできません。引き続きこの問題に取り組む場合は、問題を再現する[MCVE](https://stackoverflow.com/help/mcve)を作成することをお勧めします。 – Cheticamp

4

私によれば、このエラーが発生する理由は、のonClickListenereditTextonLongClickListenerと一緒に解雇されるということです。そして、のクリックリスナー内でpopupWindow.dismissが呼び出されているため、editTextのロングクリックリスナーコードを実行する前にポップアップウィンドウが消えてエラーが発生します。これに対する最も簡単な解決策は、あなたのonLongClickメソッドのtrueを返すようになり

: - そうすることによって

editText.setOnLongClickListener(new View.OnLongClickListener() { 
       @Override 
       public boolean onLongClick(View v) { 

        //ADD HERE ABOUT CUT COPY PASTE 
        // TODO Auto-generated method stub 
        return true; 
       } 
      }); 

、あなたが与えられた長いクリックを消費します、そして、それは、他の不必要なリスナーを起動しません。

onLongClick() - これは、 がイベントを消費したかどうかを示すブール値を返します。それ以上は持ち込まないでください。つまり、 は、イベントを処理したことを示すためにtrueを返します。 ここで停止する必要があります。あなたがそれを処理していない場合はfalseを返すか、 イベントは他のオンクリックリスナーに続行する必要があります。あなたの主な活動で

0

プライベートコンテンツmContext。

public void onCreate(){ 
mContext = this; 
} 

(getBaseContextを置き換える)mContext

LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE); 

または

LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
関連する問題