当我运行模拟器时,我的Android应用程序不断崩溃

时间:2015-02-03 22:02:05

标签: android eclipse

我应该为课程制作一个应用程序,在案例人数和帐单成本的情况下从用户那里获取输入,然后告诉你每个人添加了提示的成本是多少。事情是看起来它应该工作,但我是新的,所以不确定,但每次我在eclipse中运行模拟器它一直说我的应用程序已停止工作,请帮助。 这是我的代码。 第一次主动活动。

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

然后我的第二个活动叫做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));

            }
        });
    }
}

如果有人能提供帮助那就太棒了。有人问我这里的错误日志,我想是

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

4 个答案:

答案 0 :(得分:1)

如果你看一下产生的错误,它会为你提供一个出错的提示

  

您需要使用Theme.AppCompat主题(或后代)   活性

请查看以下链接,了解有关您收到此错误的原因以及如何解决此问题的详细信息。

You need to use a Theme.AppCompat theme (or descendant) with this activity

答案 1 :(得分:0)

您需要将AppCompat支持库添加到依赖项下的gradle:

com.android.support:appcompat-v7:21.0.+

答案 2 :(得分:0)

Stacktrace告诉你

Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

这意味着您必须为AppComapt文件中的每项活动设置AndroidManifest主题

        <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>

答案 3 :(得分:0)

日志中的关键信息是:

You need to use a Theme.AppCompat theme (or descendant) with this activity.

原因是:您的MainActivity基于ActionBarActivity,这是Android Support "appcompat" library中的一个类。在资源的某个地方,你有一个与这个库不兼容的主题。

如果您不需要支持较旧的Android版本(低至v7),则可以删除appcompat库并使用它:

public class MainActivity extends Activity {
...
}

(我将基类从ActionBarActivity替换为Activity)。

这应该解决你现在的问题。从长远来看,我建议你阅读:

Android Training也很早就引入了支持库的概念,所以它们也应该有用。