无法使用Theme.Dialog启动活动

时间:2014-10-17 09:17:21

标签: android eclipse android-activity android-theme

我不知道如何处理这个,我想我到处搜索..但这个问题只是神秘的,。我有一个名为EditdataActivity的活动

public class EditdataActivity extends ActionBarActivity{
    logclass clogs = new logclass();
    EditText edt_ln;
    EditText edt_lat;
    EditText edt_long;
    Button btn_cancel;
    Button btn_save;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dataedit);
        clogs =(logclass) getIntent().getSerializableExtra("MyClass");
        edt_ln = (EditText) findViewById(R.id.edt_Ln);
        edt_lat = (EditText) findViewById(R.id.edt_lat);
        edt_long = (EditText) findViewById(R.id.det_long);
        btn_cancel = (Button) findViewById(R.id.cancel_btn);
        btn_save = (Button) findViewById(R.id.btn_save);

        edt_ln.setText(clogs.getname());
        edt_lat.setText(clogs.getlatloc()+"");
        edt_long.setText(clogs.getlongloc()+"");

        OnClickListener savelistener = new OnClickListener() {          
            @Override
            public void onClick(View v) {
                //notifpopup("Save changes?");
             }
        };
        btn_save.setOnClickListener(savelistener);     

        OnClickListener cancellistener = new OnClickListener() {            
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(EditdataActivity.this, AdddataclassActivity.class);
                intent1.putExtra("MyClass", clogs);  
                intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);   
                startActivity(intent1);
                overridePendingTransition (R.anim.open_main, R.anim.close_next);
             }
        };
        btn_cancel.setOnClickListener(cancellistener);

    }

    void notifpopup(String notif){
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle(notif);
        alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                clogs.setname(edt_ln.getText().toString());
                double lat = Double.parseDouble(edt_lat.getText().toString());
                double longl = Double.parseDouble(edt_long.getText().toString());
                clogs.setlatloc(lat);
                clogs.setlongloc(longl);
            }
        });

        alert.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                    }
           });

            alert.show();
    }   
}

这是从这个活动中调用的

@Override
    protected void onCreate(Bundle savedInstanceState) {
        ........
        clogs =(logclass) getIntent().getSerializableExtra("MyClass");
        ........   

           Button edit_data = (Button) findViewById(R.id.btn_addattrbdel);
           OnClickListener editlistener = new OnClickListener() {           
                @Override
                public void onClick(View v) {
                    gotoeditview();
                 }
            };
            edit_data.setOnClickListener(editlistener); 
    }

    void gotoeditview(){
        Intent intentl = new Intent(this,EditdataActivity.class);
        intentl.putExtra("MyClass", clogs);  
        intentl.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        startActivity(intentl);
        overridePendingTransition (R.anim.appear, R.anim.disappear);
    }

然而,当我执行它时发生错误,如logcat beloc

所示
10-17 17:06:41.750: E/AndroidRuntime(707): FATAL EXCEPTION: main
10-17 17:06:41.750: E/AndroidRuntime(707): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ict.map_navigation/com.ict.map_navigation.EditdataActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
10-17 17:06:41.750: E/AndroidRuntime(707):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2070)
10-17 17:06:41.750: E/AndroidRuntime(707):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095)
10-17 17:06:41.750: E/AndroidRuntime(707):  at android.app.ActivityThread.access$600(ActivityThread.java:134)
10-17 17:06:41.750: E/AndroidRuntime(707):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
10-17 17:06:41.750: E/AndroidRuntime(707):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-17 17:06:41.750: E/AndroidRuntime(707):  at android.os.Looper.loop(Looper.java:137)
10-17 17:06:41.750: E/AndroidRuntime(707):  at android.app.ActivityThread.main(ActivityThread.java:4830)
10-17 17:06:41.750: E/AndroidRuntime(707):  at java.lang.reflect.Method.invokeNative(Native Method)
10-17 17:06:41.750: E/AndroidRuntime(707):  at java.lang.reflect.Method.invoke(Method.java:511)
10-17 17:06:41.750: E/AndroidRuntime(707):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
10-17 17:06:41.750: E/AndroidRuntime(707):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
10-17 17:06:41.750: E/AndroidRuntime(707):  at dalvik.system.NativeStart.main(Native Method)
10-17 17:06:41.750: E/AndroidRuntime(707): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
10-17 17:06:41.750: E/AndroidRuntime(707):  at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:110)
10-17 17:06:41.750: E/AndroidRuntime(707): ....

..然而,正如我在一些建议的解决方案上所读到的,我将EditdataActivity更改为Acitvity ..它没有任何错误..但它只显示了这个..

enter image description here

我的该活动的xml文件就是这个..

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:text="Change Name of the Location" />

    <EditText
        android:id="@+id/edt_Ln"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/edt_Ln"
        android:gravity="center"
        android:text="Change Location" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="26dp"
        android:text="Longitude:"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="28dp"
        android:text="Latitude:"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <EditText
        android:id="@+id/edt_lat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView3"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/textView4"
        android:ems="10"
        android:inputType="number" />

    <EditText
        android:id="@+id/det_long"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView4"
        android:layout_alignBottom="@+id/textView4"
        android:layout_alignLeft="@+id/edt_lat"
        android:layout_alignParentRight="true"
        android:ems="10"
        android:inputType="number" />

    <Button
        android:id="@+id/btn_save"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/det_long"
        android:layout_marginTop="17dp"
        android:layout_toRightOf="@+id/btn_cancel"
        android:background="#00ced1"
        android:text="Save" />

    <Button
        android:id="@+id/btn_cancel"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/btn_save"
        android:layout_alignBottom="@+id/btn_save"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/textView4"
        android:background="#ff0000"
        android:text="Cancel" />

</RelativeLayout>

我的其他活动与我的项目完全相同。但他们都工作正常,我试过..重新启动我的日食IDE ..但它没有工作..请帮助,也许我只是忘了一些东西。谢谢..这里是我项目的清单文件..

对不起我的长篇文章......

2 个答案:

答案 0 :(得分:1)

如果您想使用 ActionBarActivity ,其主题必须是Theme.AppCompat主题(或后代)。

如果您想使用Theme.Dialog主题,您的活动不应扩展 ActionBarActivity

答案 1 :(得分:1)

如果要创建自己的自定义对话框,请创建扩展DialogFragment而不是创建活动的类。它易于使用,您无需在清单中声明它们。

像这样使用:

public class EditDataDialog extends DialogFragment {

    // Properties here
    .....

    @Override
    public View onCreateView(LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.activity_dataedit, container, false);

        // Properties init here
        .....

        return view;
    }
}

然后,在您的主要活动中,请致电:

// Show the "edit data" dialog
FragmentManager fm = getSupportFragmentManager();
EditDataDialog editDataDialog = new EditDataDialog();
editDataDialog.show(fm, "edit_data_dialog");