请求焦点必须被称为错误android

时间:2013-10-04 00:43:05

标签: java android android-edittext

我打算从一个页面更改为另一个页面,因此第一个屏幕上有标题栏和导航栏,我想更改为另一个页面,只有textview,没有标题栏,当我离开fucos到我的的EditText。

以下是代码:

    public class MainActivity extends Activity{
     private EditText edittext;
     private ListView list;
     private CustomListAdapter customAdapter;
     private DrawerLayout drawerLayout;
     private ActionBarDrawerToggle drawerToggle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);

       // this.tv = (TextView) this.findViewById(R.id.textView1);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        //new ConnnectionRequest(tv,"95","3025");
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        drawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                drawerLayout,         /* DrawerLayout object */
                R.drawable.ic_launcher,  /* nav drawer image to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description for accessibility */
                R.string.drawer_close  /* "close drawer" description for accessibility */
                ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle("Rodolfo");
                edittext.setText("");
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle("Navalon");
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        drawerLayout.setDrawerListener(drawerToggle);

        addKeyListener(); 


        }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Pass the event to ActionBarDrawerToggle, if it returns
        // true, then it has handled the app icon touch event
        if (drawerToggle.onOptionsItemSelected(item)) {
          return true;
        }
        // Handle your other action bar items...

        return super.onOptionsItemSelected(item);
    }

    public void addKeyListener() {
        List<String> arrayStrings = new ArrayList<String>();
        arrayStrings.add("rodolfo");
        arrayStrings.add("erodolfo2");

        edittext = (EditText) findViewById(R.id.editText);
        edittext.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
        edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    //Remove title bar
                    requestWindowFeature(Window.FEATURE_NO_TITLE);

                    //Remove notification bar
                    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);



                    setContentView(R.layout.second_screen);

                }
            }
        });

        list = (ListView) findViewById(R.id.list_view);

        customAdapter = new CustomListAdapter(this,R.layout.list_item,R.id.product_name,arrayStrings);
        list.setAdapter(customAdapter);

        edittext.addTextChangedListener(new TextWatcher(){

            @Override
            public void afterTextChanged(Editable arg0) {
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {
            }

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2,
                    int arg3) {
                MainActivity.this.customAdapter.getFilter().filter(cs);

            }

        });
    }  
}

在我的addkeyListener方法中,我的edditext也已在setOnFucos更改中初始化,我的更改页面出现并出现错误。我不知道为什么它会给我一个错误。

10-03 20:47:47.916: E/AndroidRuntime(9023): FATAL EXCEPTION: main
10-03 20:47:47.916: E/AndroidRuntime(9023): **android.util.AndroidRuntimeException: requestFeature() must be called before adding content**
10-03 20:47:47.916: E/AndroidRuntime(9023):     at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:268)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.app.Activity.requestWindowFeature(Activity.java:3310)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at com.example.octranspodestination.MainActivity$2.onFocusChange(MainActivity.java:122)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.view.View.onFocusChanged(View.java:4571)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.widget.TextView.onFocusChanged(TextView.java:7640)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.widget.EditText.onFocusChanged(EditText.java:172)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.view.View.clearFocus(View.java:4461)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.view.ViewGroup.clearFocus(ViewGroup.java:775)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.view.View.setFlags(View.java:8518)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.view.View.setVisibility(View.java:5631)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.support.v4.widget.DrawerLayout$ViewDragCallback.onViewPositionChanged(DrawerLayout.java:1364)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.support.v4.widget.ViewDragHelper.continueSettling(ViewDragHelper.java:730)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.support.v4.widget.DrawerLayout.computeScroll(DrawerLayout.java:764)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.view.View.getDisplayList(View.java:12594)
10-03 20:47:47.916: E/AndroidRuntime(9023):     at android.view.View.getDisplayList(View.java:12650)

1 个答案:

答案 0 :(得分:0)

堆栈跟踪说

  

在添加内容

之前必须调用requestFeature()

查看documentation,这是真的:

  

启用扩展屏幕功能。这必须在之前调用   的setContentView()。可以根据需要多次调用它   在setContentView()之前。

onCreate()中,您已经调用setContentView(),因此当您的editText更改焦点并再次调用requestFeature时,会抛出异常。

由于在一个Activity中交换内容视图通常是不好的做法。您可能应该重新考虑您的应用程序设计 - 通常获取用户的输入不应导致状态栏和标题消失。您可以启动新的活动或改为使用对话框,但这取决于您的需求。