简体   繁体   中英

Android Error - FATAL EXCEPTION: main

I'm working on a Beginner Android assignment (an App that is supposed to buy Car Ferry tickets and calculate the cost of the tickets) and I'm having an issue with getting the App to run in the Emulator. I've gotten an example program to run in an emulator, but I can't figure out what the issue with this one is. On a side note I'm using a Mac and I'm running Eclipse if that means anything. Here is the MainActivity.java code:

package net.androidbootcamp.carferryfare;

import java.text.DecimalFormat;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.view.View.OnClickListener;



public class MainActivity extends ActionBarActivity 
{
int costPerTicket = 18;
int numberOfTickets = 0;
int totalCost = 0;
String ferryChoice = "";
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText tickets=( EditText)findViewById(R.id.txtTickets);
    final Spinner group=(Spinner)findViewById(R.id.txtGroup);
    Button cost = (Button)findViewById(R.id.btnCost);
    final TextView result = ((TextView)findViewById(R.id.txtResult));   
    cost.setOnClickListener( new OnClickListener()
    {



        @Override

        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            numberOfTickets =  Integer.parseInt(tickets.getText().toString());
            totalCost = costPerTicket * numberOfTickets;
            DecimalFormat currency = new DecimalFormat("$###,###");
            ferryChoice = group.getSelectedItem().toString();
            result.setText("Total Cost for " + ferryChoice + " is " +       currency.format(totalCost));
        }

        });

}


@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);
}
}

And here is the LogCat data that is showing up:

09-29 18:18:56.049: D/AndroidRuntime(2033): Shutting down VM
09-29 18:18:56.099: W/dalvikvm(2033): threadid=1: thread exiting with uncaught exception        (group=0xb0cd7b20)
09-29 18:18:56.159: E/AndroidRuntime(2033): FATAL EXCEPTION: main
09-29 18:18:56.159: E/AndroidRuntime(2033): Process: net.androidbootcamp.carferryfare, PID:    2033
09-29 18:18:56.159: E/AndroidRuntime(2033): java.lang.RuntimeException: Unable to start    activity     ComponentInfo{net.androidbootcamp.carferryfare/net.androidbootcamp.carferryfare.MainActivity}:    java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with   this activity.
09-29 18:18:56.159: E/AndroidRuntime(2033):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
09-29 18:18:56.159: E/AndroidRuntime(2033):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-29 18:18:56.159: E/AndroidRuntime(2033):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-29 18:18:56.159: E/AndroidRuntime(2033):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-29 18:18:56.159: E/AndroidRuntime(2033):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-29 18:18:56.159: E/AndroidRuntime(2033):     at android.os.Looper.loop(Looper.java:136)
09-29 18:18:56.159: E/AndroidRuntime(2033):     at android.app.ActivityThread.main(ActivityThread.java:5017)
09-29 18:18:56.159: E/AndroidRuntime(2033):     at java.lang.reflect.Method.invokeNative(Native Method)
09-29 18:18:56.159: E/AndroidRuntime(2033):     at java.lang.reflect.Method.invoke(Method.java:515)
09-29 18:18:56.159: E/AndroidRuntime(2033):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-29 18:18:56.159: E/AndroidRuntime(2033):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-29 18:18:56.159: E/AndroidRuntime(2033):     at dalvik.system.NativeStart.main(Native Method)
09-29 18:18:56.159: E/AndroidRuntime(2033): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
09-29 18:18:56.159: E/AndroidRuntime(2033):     at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:110)
09-29 18:18:56.159: E/AndroidRuntime(2033):     at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
09-29 18:18:56.159: E/AndroidRuntime(2033):     at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:99)
09-29 18:18:56.159: E/AndroidRuntime(2033):     at net.androidbootcamp.carferryfare.MainActivity.onCreate(MainActivity.java:27)
09-29 18:18:56.159: E/AndroidRuntime(2033):     at android.app.Activity.performCreate(Activity.java:5231)
09-29 18:18:56.159: E/AndroidRuntime(2033):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-29 18:18:56.159: E/AndroidRuntime(2033):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
09-29 18:18:56.159: E/AndroidRuntime(2033):     ... 11 more
09-29 18:19:04.359: I/Process(2033): Sending signal. PID: 2033 SIG: 9

Okay, well figured out that I need to include: import android.app.Activity; as an import in the MainActivity.java section and that's why the code wasn't working. Thank you panini and hopefully this can help anyone else that is experiencing the same issue I was.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM