Reputation: 25
I am supposed to make a app for class that takes the input from the user in the case number of people and the cost of the bill and then tells you what the cost is per person with the tip added in. The thing is it looks like it should work but i am new to this so not sure but everytime i run the emulator in eclipse it keeps saying that my app has stopped working please help. here is my code. up first mainactivity.
package com.example.splitbill;
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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.btnCalc);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Calc.class));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
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);
}
}
then my second activity which is called Calc
package com.example.splitbill;
import java.text.DecimalFormat;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class Calc extends Activity {
double totalOfBill;
int numberOfGuests;
double perPersonSplit;
String groupChoice;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.calc);
final EditText guests=(EditText)findViewById(R.id.txtGuests);
final EditText bill=(EditText)findViewById(R.id.txtBill);
final Spinner group = (Spinner)findViewById(R.id.txtGroup);
Button cost = (Button)findViewById(R.id.btnSplit);
cost.setOnClickListener(new OnClickListener() {
final TextView result = ((TextView)findViewById(R.id.txtResult));
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
totalOfBill = Double.parseDouble(bill.getText().toString());
numberOfGuests = Integer.parseInt(guests.getText().toString());
perPersonSplit = (totalOfBill * .18 +totalOfBill) / numberOfGuests;
DecimalFormat currency = new DecimalFormat("$###,###.##");
groupChoice = group.getSelectedItem().toString();
result.setText("Quality is " + groupChoice + "cost is " + currency.format(perPersonSplit));
}
});
}
}
If anyone can help that would be awesome. some one asked me for the error log here it is i think
02-03 16:53:14.297: D/AndroidRuntime(1043): Shutting down VM
02-03 16:53:14.297: D/AndroidRuntime(1043): --------- beginning of crash
02-03 16:53:14.312: E/AndroidRuntime(1043): FATAL EXCEPTION: main
02-03 16:53:14.312: E/AndroidRuntime(1043): Process: com.example.splitbill, PID: 1043
02-03 16:53:14.312: E/AndroidRuntime(1043): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.splitbill/com.example.splitbill.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
02-03 16:53:14.312: E/AndroidRuntime(1043): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
02-03 16:53:14.312: E/AndroidRuntime(1043): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
02-03 16:53:14.312: E/AndroidRuntime(1043): at android.app.ActivityThread.access$800(ActivityThread.java:144)
02-03 16:53:14.312: E/AndroidRuntime(1043): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
02-03 16:53:14.312: E/AndroidRuntime(1043): at android.os.Handler.dispatchMessage(Handler.java:102)
02-03 16:53:14.312: E/AndroidRuntime(1043): at android.os.Looper.loop(Looper.java:135)
02-03 16:53:14.312: E/AndroidRuntime(1043): at android.app.ActivityThread.main(ActivityThread.java:5221)
02-03 16:53:14.312: E/AndroidRuntime(1043): at java.lang.reflect.Method.invoke(Native Method)
02-03 16:53:14.312: E/AndroidRuntime(1043): at java.lang.reflect.Method.invoke(Method.java:372)
02-03 16:53:14.312: E/AndroidRuntime(1043): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-03 16:53:14.312: E/AndroidRuntime(1043): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-03 16:53:14.312: E/AndroidRuntime(1043): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
02-03 16:53:14.312: E/AndroidRuntime(1043): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
02-03 16:53:14.312: E/AndroidRuntime(1043): at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
02-03 16:53:14.312: E/AndroidRuntime(1043): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
02-03 16:53:14.312: E/AndroidRuntime(1043): at com.example.splitbill.MainActivity.onCreate(MainActivity.java:16)
02-03 16:53:14.312: E/AndroidRuntime(1043): at android.app.Activity.performCreate(Activity.java:5933)
02-03 16:53:14.312: E/AndroidRuntime(1043): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-03 16:53:14.312: E/AndroidRuntime(1043): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
02-03 16:53:14.312: E/AndroidRuntime(1043): ... 10 more
Upvotes: 0
Views: 164
Reputation: 544
The key message from the log is this:
You need to use a Theme.AppCompat theme (or descendant) with this activity.
The reason is: your MainActivity
is based on ActionBarActivity
, which is a class from the Android Support "appcompat" library. And somewhere in your resources you have a theme which is incompatible with this library.
If you don't need to support older Android versions (down to v7), you could drop the appcompat library and use this instead:
public class MainActivity extends Activity {
...
}
(I replaced the base class from ActionBarActivity
to Activity
).
This should solve your problem for now. In the long run, I would recommend you read up on:
Android Training also introduces the concept of the support library quite early, so they should be helpful as well.
Upvotes: 0
Reputation: 1902
Stacktrace told you
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
It's mean that you have to set AppComapt
theme for each activity in AndroidManifest
file
<activity
android:name=".ui.activity.MainActivity"
android:theme="@style/Theme.AppCompat"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Upvotes: 0
Reputation: 5757
If you look at the error produced it provides you with a hint to what is going wrong
You need to use a Theme.AppCompat theme (or descendant) with this activity
Take a look at the link below for more information about why you are getting this error and how you go about fixing it.
You need to use a Theme.AppCompat theme (or descendant) with this activity
Upvotes: 1
Reputation: 4835
You need to add the AppCompat support library to your gradle under dependencies:
com.android.support:appcompat-v7:21.0.+
Upvotes: 0