Vivian
Vivian

Reputation: 1

First program doesn't work and i have no idea why

I'm an total beginner in programming from germany. I wanted to create a program where you can calculate the costs of gas while driving somewhere. So i wanted a 'starting page', a 'calculating page' and a toast that gives the result. i have two activities. I'm not sure why my program doesn't work! I hope someone can help me because this will be my exam and it's very important for me to 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. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a 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 also 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 the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.berechnungsactivity, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


}

The xml file to the 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 to start activity ComponentInfo{de.vivian.calci/de.vivian.calci.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
06-19 07:22:10.393: E/AndroidRuntime(775):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-19 07:22:10.393: E/AndroidRuntime(775):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-19 07:22:10.393: E/AndroidRuntime(775):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-19 07:22:10.393: E/AndroidRuntime(775):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-19 07:22:10.393: E/AndroidRuntime(775):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 07:22:10.393: E/AndroidRuntime(775):  at android.os.Looper.loop(Looper.java:137)
06-19 07:22:10.393: E/AndroidRuntime(775):  at android.app.ActivityThread.main(ActivityThread.java:5041)
06-19 07:22:10.393: E/AndroidRuntime(775):  at java.lang.reflect.Method.invokeNative(Native Method)
06-19 07:22:10.393: E/AndroidRuntime(775):  at java.lang.reflect.Method.invoke(Method.java:511)
06-19 07:22:10.393: E/AndroidRuntime(775):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-19 07:22:10.393: E/AndroidRuntime(775):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-19 07:22:10.393: E/AndroidRuntime(775):  at dalvik.system.NativeStart.main(Native Method)
06-19 07:22:10.393: E/AndroidRuntime(775): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
06-19 07:22:10.393: E/AndroidRuntime(775):  at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:113)
06-19 07:22:10.393: E/AndroidRuntime(775):  at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
06-19 07:22:10.393: E/AndroidRuntime(775):  at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59)
06-19 07:22:10.393: E/AndroidRuntime(775):  at de.vivian.calci.MainActivity.onCreate(MainActivity.java:25)
06-19 07:22:10.393: E/AndroidRuntime(775):  at android.app.Activity.performCreate(Activity.java:5104)
06-19 07:22:10.393: E/AndroidRuntime(775):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-19 07:22:10.393: E/AndroidRuntime(775):  at 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. This theme is replaced 
        by AppBaseTheme from 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 to backward-compatibility can go here. -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level 
            can go here. -->



        <item name="android:windowNoTitle">true</item> <!-- Hides the Action Bar test -->
        <item name="android:windowFullscreen">true</item> <!-- Hides the status bar test -->




    </style>

</resources>

Upvotes: 0

Views: 83

Answers (4)

capt.swag
capt.swag

Reputation: 10661

You are using

android:theme="@android:style/Theme.Light"

as the theme of your application. Since your 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 to backward-compatibility can go here. -->
</style>

You should be using AppBaseTheme as your theme in your manifest.

android:theme="@android:style/AppBaseTheme

Upvotes: 1

Dennis Anderson
Dennis Anderson

Reputation: 1396

The reason you are having this problem is because the activity you are trying to apply the theme to is extending ActionBarActivity which requires the AppCompat theme to be applied.

Change the Java inheritance from ActionBarActivity to Activity and leave the theme in the manifest as it is.

Upvotes: 1

Chandrakanth
Chandrakanth

Reputation: 3831

In your manifest.xml file for <application> tag change theme attribute like this

android:theme="@style/AppTheme"

Upvotes: 0

Gabriella Angelova
Gabriella Angelova

Reputation: 2985

According to the error you get, you should change this line :

public class Berechnungsactivity extends ActionBarActivity{

with this one:

public class Berechnungsactivity extends Activity{

and everything else will be the same like it is

Upvotes: 1

Related Questions