如何解决requestFeature()必须在android中添加内容之前调用

时间:2015-02-02 07:59:15

标签: android database sqlite

我正在创建一个应用程序,它包含单击按钮时在警告对话框中弹出的简单和小型注册表单。但它显示一个名为 android.util.AndroidRuntimeException的错误:在添加内容之前必须调用requestFeature()请让它解决

这里我也使用了SQLite数据库 这是我的活动

package com.example.androidmaterial;

import com.gc.materialdesign.views.ButtonFlat;
import com.gc.materialdesign.views.ButtonFloat;
import com.gc.materialdesign.views.ButtonRectangle;
import com.gc.materialdesign.widgets.Dialog;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Sample extends Activity
{
    ButtonFloat keyboard;
    ButtonRectangle hide_keyboard;
    LoginDataBaseAdapter login_database_adapter;
    ButtonFlat database;
    EditText name;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_sample);
        login_database_adapter = new LoginDataBaseAdapter(Sample.this);
        login_database_adapter = login_database_adapter.open();

        database = (ButtonFlat)findViewById(R.id.buttonflat);
        database.setOnClickListener(new View.OnClickListener() 
        {

            @Override
            public void onClick(View v) 
            {
                Dialog dialog_signup = new Dialog(Sample.this,"Registration","Please Fill all the details");
                dialog_signup.setContentView(R.layout.activity_signup);
                final EditText username = (EditText)dialog_signup.findViewById(R.id.editText1);
                final EditText password =  (EditText)dialog_signup.findViewById(R.id.editText2);
                final EditText confirmpass = (EditText)dialog_signup.findViewById(R.id.editText3);
                final EditText name = (EditText)dialog_signup.findViewById(R.id.editText4);
                final ButtonFlat register  = (ButtonFlat)dialog_signup.findViewById(R.id.buttonflat);
                register.setOnClickListener(new View.OnClickListener() 
                {

                    @Override
                    public void onClick(View v) 
                    {
                        String user_name = name.getText().toString(); 
                        String mqs_user_name = username.getText().toString();
                        String mqs_password = password.getText().toString();
                        String mqs_confirmpass =  confirmpass.getText().toString();

                        if(mqs_user_name.equals("")||mqs_password.equals("")||mqs_confirmpass.equals(""))
                        {
                            Toast.makeText(Sample.this, "Some fields are empty please enter all the fields",Toast.LENGTH_LONG).show();
                        }
                        if(mqs_password!=mqs_confirmpass)
                        {
                            Toast.makeText(Sample.this, "Passwords did not match",Toast.LENGTH_LONG).show();
                        }
                        else
                        {

                            login_database_adapter.insertEntry(mqs_user_name, mqs_password);
                            Toast.makeText(Sample.this, "Account Successfully created"+" "+user_name,Toast.LENGTH_LONG).show();
                        }


                    }
                });
                dialog_signup.show();

            }
        });


        keyboard = (ButtonFloat)findViewById(R.id.buttonFloat);
        hide_keyboard = (ButtonRectangle)findViewById(R.id.button_2);
        hide_keyboard.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) 
            {try{
                InputMethodManager imm_2 = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm_2.hideSoftInputFromInputMethod(getCurrentFocus().getWindowToken(),0);
            }
            catch(Exception e)
            {
                Toast.makeText(Sample.this, e.getMessage(), Toast.LENGTH_LONG).show();
            }

            }
        });
        keyboard.requestFocus();
        keyboard.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Dialog dialog = new Dialog(Sample.this, "MedeQuip", "Hello...This is a sample dialog box check");
                dialog.setOnAcceptButtonClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Toast.makeText(Sample.this, "Click accept button", 1).show();
                    }
                });
                dialog.setOnCancelButtonClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Toast.makeText(Sample.this, "Click cancel button", 1).show();
                    }
                });
                dialog.show();
            }
        });

    }
@Override
protected void onDestroy() {


    login_database_adapter.close();
    super.onDestroy();
}
}

修改 她是我使用的代码

protected void onCreate(Bundle savedInstanceState){ 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_sample); 
    login_database_adapter = new LoginDataBaseAdapter(Sample.this);  
    login_database_adapter = login_database_adapter.open();
}

这是我的Logcat

02-02 15:28:32.106: E/AndroidRuntime(19240): FATAL EXCEPTION: main
02-02 15:28:32.106: E/AndroidRuntime(19240): Process: com.example.androidmaterial, PID: 19240
02-02 15:28:32.106: E/AndroidRuntime(19240): android.util.AndroidRuntimeException: requestFeature() must be called before adding content
02-02 15:28:32.106: E/AndroidRuntime(19240):    at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:264)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.app.Dialog.requestWindowFeature(Dialog.java:1041)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at com.gc.materialdesign.widgets.Dialog.onCreate(Dialog.java:56)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.app.Dialog.dispatchOnCreate(Dialog.java:361)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.app.Dialog.show(Dialog.java:262)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at com.gc.materialdesign.widgets.Dialog.show(Dialog.java:111)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at com.example.androidmaterial.Sample$1.onClick(Sample.java:81)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at com.gc.materialdesign.views.ButtonFlat.onDraw(ButtonFlat.java:83)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.View.draw(View.java:14728)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.View.getDisplayList(View.java:13588)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.View.getDisplayList(View.java:13635)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3226)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.View.getDisplayList(View.java:13509)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.View.getDisplayList(View.java:13635)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3226)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.View.getDisplayList(View.java:13509)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.View.getDisplayList(View.java:13635)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3226)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.View.getDisplayList(View.java:13509)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.View.getDisplayList(View.java:13635)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3226)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.View.getDisplayList(View.java:13509)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.View.getDisplayList(View.java:13635)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1627)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1506)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.ViewRootImpl.draw(ViewRootImpl.java:2722)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2587)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2210)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1189)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6223)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.Choreographer.doCallbacks(Choreographer.java:591)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.Choreographer.doFrame(Choreographer.java:560)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.os.Handler.handleCallback(Handler.java:808)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.os.Handler.dispatchMessage(Handler.java:103)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.os.Looper.loop(Looper.java:193)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at android.app.ActivityThread.main(ActivityThread.java:5292)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at java.lang.reflect.Method.invokeNative(Native Method)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at java.lang.reflect.Method.invoke(Method.java:515)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
02-02 15:28:32.106: E/AndroidRuntime(19240):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

问题是由MaterialDesignLibrary导致的。由于Dialog类中的以下行:

@Override
protected void onCreate(Bundle savedInstanceState) {
 requestWindowFeature(Window.FEATURE_NO_TITLE);
 ....
}

此处requestWindowFeature在调用Activity中的setContentView方法后调用。

要解决此问题,您应该从requestWindowFeature(Window.FEATURE_NO_TITLE);类库中删除Dialog