我正在进行初学者Android任务(一个应该购买Car Ferry门票并计算门票费用的App),我遇到了让App在模拟器中运行的问题。我已经有了一个在模拟器中运行的示例程序,但我无法弄清楚这个问题是什么。从侧面说明我正在使用Mac,如果这意味着什么,我正在运行Eclipse。这是MainActivity.java代码:
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);
}
}
以下是显示的LogCat数据:
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
答案 0 :(得分:1)
好的,我很清楚我需要在MainActivity.java部分中包含:import android.app.Activity;
作为导入,这就是代码无效的原因。谢谢panini,希望这可以帮助其他遇到同样问题的人。