5

Tôi đang cố gắng thực hiện một cú nhấp chuột dài trong một EditText, nhưng khi tôi nhấp vào Dài, tôi nhận được lỗi bên dưới. Tôi muốn có thể thực hiện một cú nhấp chuột dài để có được bối cảnh Copy/Paste/Select All bật lên để người dùng có thể dán văn bản vào hộp.Nhấn và giữ Sửa lỗi văn bản: Không thể thêm cửa sổ

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

EditText nằm trong ScrollView trong cửa sổ bật lên. Vì vậy, khi lỗi xảy ra, tôi hiện đang hoạt động trên một hoạt động với PopupWindow mở và tôi làm một Long Click trong EditText được chứa trên PopupWindow.

Gradle Cài đặt

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

Layout Chứa các EditText:

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

Pop lên cửa sổ:

@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

Bạn có đang gọi mã này trong Hoạt động không? Một dịch vụ? Bạn có chắc chắn bạn không bị tắt màn hình nếu một Hoạt động? Thông báo đó thường xảy ra khi bạn cố gắng khởi chạy một giao diện người dùng từ một Dịch vụ hoặc khi bạn cố gắng bật lên một hộp thoại sau khi hoạt động được thực hiện. –

+0

Tôi đang gọi mã cho cửa sổ bật lên trong khi đang hoạt động. Hoạt động hiện đang chạy, cửa sổ bật lên đang hoạt động và tôi nhấn và giữ trong EditText và nhận được lỗi. – Adam

+1

Tôi chạy mã của bạn với sửa đổi nhỏ trên một trình giả lập chạy API 24 và nó hoạt động OK cho tôi. Đó là để nói rằng nó không phải là crashing và người nghe báo chí dài được gọi như mong đợi. Bạn có thể cung cấp thêm một số thông tin về cách bạn đang thiết lập mọi thứ không? API bạn đang thử nghiệm? [Ở đây] (https://gist.github.com/Cheticamp/08ebef491a727a12577a7e9790eaa752) là một ý chính của hoạt động tôi đã sử dụng trong trường hợp nó giúp bạn. – Cheticamp

Trả lời

0

Chúng tôi đã đi với một thiết kế lại để lấy đi các popup và loại bỏ những lỗi đó là ở đó.

0

Bạn có thể đang gọi popup của bạn quá sớm. Trong ví dụ của tôi (gist here), tôi đang thực thi mã popup từ một nút bấm. Đây là sau onCreate() và các phương thức vòng đời quan trọng khác chạy. Với ví dụ, mọi thứ hoạt động OK.

Nếu, tuy nhiên, tôi cố gắng tạo nhanh cửa sổ bật lên trong onCreate(), tôi nhận được lỗi logcat cho biết: "android.view.WindowManager $ BadTokenException: Không thể thêm cửa sổ" là những gì bạn đang thấy.

Tôi tin rằng bạn đang cố gắng tạo nhanh cửa sổ bật lên của mình quá sớm. Bắt đầu nó sau này trong vòng đời của hoạt động và chắc chắn sau khi thực hiện onCreate(). Nếu bạn cần khởi tạo nó ngay, bạn có thể đính kèm mã vào hàng đợi thông điệp UI qua cuộc gọi đến post(Runnable). (Xem here).

Tôi khá chắc chắn rằng đây là vấn đề của bạn. Nếu điều này không hữu ích, hãy cập nhật câu hỏi của bạn với nhiều thông tin hơn về cách thức và khi bạn khởi tạo cửa sổ bật lên.

+0

Tôi đã thêm nhiều mã vào câu hỏi để bạn có thể xem thêm về những gì đang diễn ra. – Adam

+0

@Adam Tôi không thể tạo lại vấn đề của bạn với những gì bạn đã cung cấp. Nếu bạn tiếp tục gặp vấn đề này, tôi khuyên bạn nên tạo [MCVE] (https://stackoverflow.com/help/mcve) để tái tạo sự cố. – Cheticamp

4

Theo tôi, lý do bạn nhận được lỗi này là của onClickListener bị sa thải cùng với onLongClickListener trong số editText của bạn. Và, kể từ khi popupWindow.dismiss được gọi bên trong trình nghe nhấp chuột của outer_layout, cửa sổ bật lên bị loại bỏ trước khi mã người nghe nhấp chuột dài editText của bạn có thể chạy, do đó gây ra lỗi.

Giải pháp đơn giản nhất để điều này sẽ quay trở lại true cho phương pháp onLongClick của bạn: -

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

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

Bằng cách đó, bạn sẽ tiêu thụ các nhấp chuột dài nhất định, và nó sẽ không cháy lên bất kỳ người nghe không mong muốn khác .

onLongClick() - Điều này trả về giá trị boolean để cho biết bạn có đã tiêu thụ sự kiện và không nên thực hiện thêm. Tức là, trả lại giá trị true để cho biết rằng bạn đã xử lý sự kiện và cần dừng tại đây; trả về false nếu bạn chưa xử lý và/hoặc sự kiện sẽ tiếp tục với bất kỳ người nghe nhấp chuột nào khác.

0

trong Hoạt động chính của bạn

tin Content mContext;

public void onCreate(){ 
mContext = this; 
} 

thay getBaseContext() với mContext

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

hoặc

LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
Các vấn đề liên quan