android - First program doesn't work and i have no idea why -
i'm total beginner in programming germany. wanted create program can calculate costs of gas while driving somewhere. wanted 'starting page', 'calculating page' , toast gives result. have 2 activities. i'm not sure why program doesn't work! hope can me because exam , it's important me work.
the main activity:
package de.vivian.calci; import android.support.v7.app.actionbaractivity; import android.content.intent; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import.android.view.view.onclicklistener; import android.widget.button; public class mainactivity extends actionbaractivity implements view.onclicklistener{ private button beginnen; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //der button für den beginn der berechnun beginnen= (button) findviewbyid(r.id.buttonbeginnen); beginnen.setonclicklistener(this); } @override public void onclick(view v) { // todo auto-generated method stub intent intent = new intent(this, berechnungsactivity.class); startactivity(intent); //bei drücken des buttons soll die berechnungsactivity über ein intent aufgerufen werden } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } } the activity_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#cbd0d0" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="de.vivian.calci.mainactivity" > <button android:id="@+id/buttonbeginnen" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:layout_marginbottom="100dp" android:text="@string/beginnen" android:textcolor="#fa5882" /> <imageview android:id="@+id/imageview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:layout_marginbottom="350dp" android:contentdescription="@string/description" android:src="@drawable/auto" /> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_aligntop="@+id/imageview1" android:layout_centerhorizontal="true" android:layout_margintop="138dp" android:gravity="center_horizontal" android:text="@string/begruessung" android:textcolor="#000000" android:textsize="@dimen/textview_schriftgroesse" /> </relativelayout> the second activity named berechnungsactivity (calculatingactivity)
package de.vivian.calci; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view.onclicklistener; import android.widget.button; import android.widget.textview; import android.widget.toast; import android.view.view; public class berechnungsactivity extends actionbaractivity{ private button buttonspritkosten; private button buttonabbrechen; //die möglichen buttons in dieser activity private onclicklistener listi= new onclicklistener(){ //hier konnte ich mit dem onclicklistener nur über ein objekt arbeiten, da es mir sonst einen fehler angezeigt hat @override public void onclick(view v) { if (v == buttonspritkosten) { textview textviewpersonenzahl; textview textviewstrecke; textview textviewverbrauch; textview textviewpreis; double berechnung; string personenzahl; string strecke; string verbrauch; string preis; // eingabefelder auslesen textviewpersonenzahl = (textview) findviewbyid(r.id.editpersonen); textviewstrecke = (textview) findviewbyid(r.id.editstrecke); textviewverbrauch = (textview) findviewbyid(r.id.editverbrauch); textviewpreis = (textview) findviewbyid(r.id.editpreis); personenzahl=textviewpersonenzahl.gettext().tostring(); strecke=textviewstrecke.gettext().tostring(); verbrauch=textviewverbrauch.gettext().tostring(); preis=textviewpreis.gettext().tostring(); berechnung= (((((double.parsedouble(strecke))/100)*(double.parsedouble(verbrauch)))*(double.parsedouble(preis)))/(double.parsedouble(personenzahl))); toast eintoast = toast.maketext(v.getcontext(), "die kosten pro person betragen:" + string.valueof(berechnung), toast.length_short); eintoast.show(); ; //wenn hier der button berechnen gedrückt wird, sollen die kosten berechnet werden und ein toast mit dem ergebnis ausgegeben werden } else if (v == buttonabbrechen) { finish(); // sollte der user abbrechen drücken, soll die app beendet werden } // todo auto-generated method stub } }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_berechnungsactivity); buttonspritkosten= (button) findviewbyid(r.id.buttonspritkosten); buttonspritkosten.setonclicklistener(listi); buttonabbrechen= (button) findviewbyid(r.id.buttonabbrechen); buttonabbrechen.setonclicklistener(listi); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.berechnungsactivity, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } } the xml file second activity berechnungsactivity (calculating activity)
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:background="#cbd0d0" tools:context="de.vivian.calci.berechnungsactivity" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:text="bitte fülle folgende felder aus:" android:textsize="@dimen/textview_schriftgroesse" android:textcolor="#fa5882" /> <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/textview1" android:layout_below="@+id/textview1" android:layout_margintop="50dp" android:text="personenzahl" android:textappearance="?android:attr/textappearancesmall" android:textcolor="@color/black" /> <textview android:id="@+id/textview3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/textview2" android:layout_below="@+id/textview2" android:layout_margintop="50dp" android:text="strecke in km" android:textappearance="?android:attr/textappearancesmall" android:textcolor="@color/black"/> <textview android:id="@+id/textview4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/textview3" android:layout_below="@+id/textview3" android:layout_margintop="50dp" android:text="verbrauch in l" android:textappearance="?android:attr/textappearancesmall" android:textcolor="@color/black" /> <textview android:id="@+id/textview5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/textview4" android:layout_below="@+id/textview4" android:layout_margintop="50dp" android:text="literpreis in €" android:textappearance="?android:attr/textappearancesmall" android:textcolor="@color/black" /> <!-- länge des eingabefeldes --> <button android:id="@+id/buttonspritkosten" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:layout_marginbottom="110dp" android:text="@string/buttonspritkosten" android:textcolor="#fa5882" /> <edittext android:id="@+id/editpersonen" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/textview2" android:layout_alignright="@+id/textview1" android:ems="6" android:inputtype="numberdecimal" /> <edittext android:id="@+id/editstrecke" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/textview3" android:layout_alignleft="@+id/editpersonen" android:ems="6" android:inputtype="numberdecimal" > <requestfocus /> </edittext> <edittext android:id="@+id/editverbrauch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/textview4" android:layout_alignleft="@+id/editstrecke" android:ems="6" android:inputtype="numberdecimal" /> <edittext android:id="@+id/editpreis" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/textview5" android:layout_alignleft="@+id/editverbrauch" android:ems="6" android:inputtype="numberdecimal" /> <button android:id="@+id/buttonabbrechen" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_aligntop="@+id/buttonspritkosten" android:layout_centerhorizontal="true" android:layout_margintop="50dp" android:text="@string/buttonabbrechen" android:textcolor="#fa5882" /> </relativelayout> the manifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="de.vivian.calci" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="16" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@android:style/theme.light"> > <activity android:name=".mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".berechnungsactivity" android:label="@string/title_activity_berechnungsactivity" > </activity> </application> </manifest> logcat says:
06-19 07:22:10.393: e/androidruntime(775): fatal exception: main 06-19 07:22:10.393: e/androidruntime(775): java.lang.runtimeexception: unable start activity componentinfo{de.vivian.calci/de.vivian.calci.mainactivity}: java.lang.illegalstateexception: need use theme.appcompat theme (or descendant) activity. 06-19 07:22:10.393: e/androidruntime(775): @ android.app.activitythread.performlaunchactivity(activitythread.java:2180) 06-19 07:22:10.393: e/androidruntime(775): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 06-19 07:22:10.393: e/androidruntime(775): @ android.app.activitythread.access$600(activitythread.java:141) 06-19 07:22:10.393: e/androidruntime(775): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 06-19 07:22:10.393: e/androidruntime(775): @ android.os.handler.dispatchmessage(handler.java:99) 06-19 07:22:10.393: e/androidruntime(775): @ android.os.looper.loop(looper.java:137) 06-19 07:22:10.393: e/androidruntime(775): @ android.app.activitythread.main(activitythread.java:5041) 06-19 07:22:10.393: e/androidruntime(775): @ java.lang.reflect.method.invokenative(native method) 06-19 07:22:10.393: e/androidruntime(775): @ java.lang.reflect.method.invoke(method.java:511) 06-19 07:22:10.393: e/androidruntime(775): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 06-19 07:22:10.393: e/androidruntime(775): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 06-19 07:22:10.393: e/androidruntime(775): @ dalvik.system.nativestart.main(native method) 06-19 07:22:10.393: e/androidruntime(775): caused by: java.lang.illegalstateexception: need use theme.appcompat theme (or descendant) activity. 06-19 07:22:10.393: e/androidruntime(775): @ android.support.v7.app.appcompatdelegateimplbase.oncreate(appcompatdelegateimplbase.java:113) 06-19 07:22:10.393: e/androidruntime(775): @ android.support.v7.app.appcompatdelegateimplv7.oncreate(appcompatdelegateimplv7.java:146) 06-19 07:22:10.393: e/androidruntime(775): @ android.support.v7.app.appcompatactivity.oncreate(appcompatactivity.java:59) 06-19 07:22:10.393: e/androidruntime(775): @ de.vivian.calci.mainactivity.oncreate(mainactivity.java:25) 06-19 07:22:10.393: e/androidruntime(775): @ android.app.activity.performcreate(activity.java:5104) 06-19 07:22:10.393: e/androidruntime(775): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 06-19 07:22:10.393: e/androidruntime(775): @ android.app.activitythread.performlaunchactivity(activitythread.java:2144) 06-19 07:22:10.393: e/androidruntime(775): ... 11 more styles.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- base application theme, dependent on api level. theme replaced appbasetheme res/values-vxx/styles.xml on newer devices. --> <style name="appbasetheme" parent="theme.appcompat.light"> <!-- theme customizations available in newer api levels can go in res/values-vxx/styles.xml, while customizations related backward-compatibility can go here. --> </style> <!-- application theme. --> <style name="apptheme" parent="appbasetheme"> <!-- customizations not specific particular api-level can go here. --> <item name="android:windownotitle">true</item> <!-- hides action bar test --> <item name="android:windowfullscreen">true</item> <!-- hides status bar test --> </style> </resources>
you using
android:theme="@android:style/theme.light" as theme of application. since styles.xml has
<style name="appbasetheme" parent="theme.appcompat.light"> <!-- theme customizations available in newer api levels can go in res/values-vxx/styles.xml, while customizations related backward-compatibility can go here. --> </style> you should using appbasetheme theme in manifest.
android:theme="@android:style/appbasetheme
Comments
Post a Comment