[英]my android app keeps crashing when i run the emulator
我應該為班級制作一個應用程序,該應用程序可以從用戶輸入案例數量和賬單成本,然后告訴您每人成本加上小費是多少。事情看起來像它應該可以工作,但是我對此並不陌生,所以不確定,但是每次我在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
如果您查看產生的錯誤,它會提示您發生了什么問題
您需要在此活動中使用Theme.AppCompat主題(或后代)
請查看下面的鏈接,以獲取有關為什么會出現此錯誤以及如何進行修復的更多信息。
您需要在依賴項下將AppCompat支持庫添加到gradle中:
com.android.support:appcompat-v7:21.0.+
Stacktrace告訴你
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
這意味着您必須為AndroidManifest
文件中的每個活動設置AppComapt
主題
<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>
日志中的關鍵消息是:
You need to use a Theme.AppCompat theme (or descendant) with this activity.
原因是:您的MainActivity
基於ActionBarActivity
,它是Android支持的“ appcompat”庫中的類 。 在您的資源中的某個地方,您有一個與此庫不兼容的主題。
如果您不需要支持較早的Android版本(低於v7),則可以刪除appcompat庫並使用它:
public class MainActivity extends Activity {
...
}
(我將基類從ActionBarActivity
替換為Activity
)。
現在應該可以解決您的問題。 從長遠來看,我建議您繼續閱讀以下內容:
Android培訓還很早就引入了支持庫的概念,因此它們也應有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.