Android:使用片段在OnEditorActionListener事件上的AutoCompleteTextView上发生崩溃

时间:2014-04-25 21:17:31

标签: android android-viewpager fragment freeze autocompletetextview

我正在尝试使用带有ViewPager导航的Fragment和ActionBar使用OnEditorActionListener事件来更改UI:

这是用于设置Fragment布局的MainLayout.class:

public class MainLayout extends LayoutCreator{

public AutoCompleteTextView editTextSearch;
public ImageView imageViewSearch;
public ImageView imageViewLogo;
public WebView webViewCenter;

public MainLayout(View rootView){
    super(rootView);
}

public void setView(){
    String[] cities = rootView.getResources().getStringArray(R.array.centerNames);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(rootView.getContext(), android.R.layout.simple_list_item_1, cities);
    editTextSearch = (MyAutoCompleteTextView) rootView.findViewById(R.id.editTextSearch);
    webViewCenter = (WebView) rootView.findViewById(R.id.webViewCenter);
    webViewCenter.setWebChromeClient(new WebChromeClient());
    webViewCenter.getSettings().setJavaScriptEnabled(true);
    webViewCenter.getSettings().setLoadWithOverviewMode(true);
    webViewCenter.getSettings().setUseWideViewPort(true);
    webViewCenter.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    webViewCenter.getSettings().setAllowFileAccess(true);
    webViewCenter.loadUrl("http://www.ccmitalia.unito.it/index.php?option=com_content&view=article&id=59&Itemid=5");
    webViewCenter.setWebChromeClient(new WebChromeClient());
    webViewCenter.getSettings().setSupportZoom(true);
    webViewCenter.getSettings().setBuiltInZoomControls(true);
    editTextSearch.setAdapter(adapter);
    editTextSearch.setThreshold(0);
    editTextSearch.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            editTextSearch.showDropDown();
        }
    });
    editTextSearch.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if(event.getKeyCode() == KeyEvent.KEYCODE_SEARCH){
                webViewCenter.setVisibility(View.VISIBLE);
            }
            return false;
        }
    });
    rootView.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            Log.i("test","test3");
            if(keyCode == KeyEvent.KEYCODE_BACK){
                if(webViewCenter.getVisibility() == View.VISIBLE)
                    webViewCenter.setVisibility(View.INVISIBLE);
            }
            return false;
        }
    });
    imageViewSearch = (ImageView) rootView.findViewById(R.id.imageViewSearch);
    imageViewLogo = (ImageView) rootView.findViewById(R.id.imageViewLogo);

    editTextSearch.setHint("Cerca centro clinico");
}

这就是我在MainActivity.class中创建Fragment的方式:

public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);

        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        FragmentSelector fragmentSelector = new FragmentSelector(getArguments().getInt(ARG_SECTION_NUMBER));
        View rootView = inflater.inflate(fragmentSelector.getFragmentResourceItem(), container, false);
        fragmentSelector.setLayout(rootView);
        return rootView;
    }
}

这就是我选择片段在FragmentSelector.class中使用的方式:

public class FragmentSelector {

private int fragmentNumber;

public FragmentSelector(int fragmentNumber){
    this.fragmentNumber = fragmentNumber;
}

public int getFragmentResourceItem(){
    switch(fragmentNumber){
        case 0: return R.layout.fragment_main;
        case 1: return R.layout.fragment_about_us;
        case 2: return R.layout.fragment_video;
        case 3: return R.layout.fragment_news;
        case 4: return R.layout.fragment_maps;
        default: return 0;
    }
}

public void setLayout(View rootView){
    switch(fragmentNumber){
        case 0: new MainLayout(rootView); break;
        case 1: new AboutUsLayout(rootView); break;
        case 2: new VideoLayout(rootView); break;
        case 3: new NewsLayout(rootView); break;
        case 4: new MapsLayout(rootView); break;
    }
}

崩溃是由 webViewCenter.setVisibility(View.VISIBLE);

该应用程序冻结,我无法从LogCat获取任何错误信息。

按下搜索键时应用程序崩溃 图片: https://lh5.googleusercontent.com/-RhULws7JRAg/U1rPmim4p7I/AAAAAAAAECE/sCtI6UYVuJQ/w343-h609-no/2014+-+1

解决方案

我已经改变了这样的onEditorAction方法:

editTextSearch.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if(actionId == EditorInfo.IME_ACTION_SEARCH){
                webViewCenter.setVisibility(View.VISIBLE);
            }
            return false;
        }
});

现在一切似乎都有效。

0 个答案:

没有答案