我正在学习android开发,我已经为练习制作了一个计算器应用程序。它在纵向模式下工作正常。因此,在应用程序甚至无法以纵向模式工作后,我为横向创建了一个新的xml文件。一旦它启动,它就会崩溃并显示消息“遗憾的是应用程序已停止工作”。这是我的代码
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mabhi_000.myapplication" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="@android:style/Theme.Holo">
</style>
</resources>
MainActivity.java
package com.example.mabhi_000.myapplication;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.TextView;
import java.util.EmptyStackException;
import java.util.Stack;
public class MainActivity extends ActionBarActivity {
public char[] ch1=new char[20];
public char[] tempch=new char[20];
int result=0,i,len,j;
String char1="",temp,t="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void one(View view) {
char1+="1";
display();
}
public void two(View view) {
char1+="2";
display();
}
public void three(View view) {
char1+="3";
display();
}
public void four(View view) {
char1+="4";
display();
}
public void five(View view) {
char1+="5";
display();
}
public void six(View view) {
char1+="6";
display();
}
public void seven(View view) {
char1+="7";
display();
}
public void eight(View view) {
char1+="8";
display();
}
public void nine(View view) {
char1+="9";
display();
}
public void zero(View view) {
char1+="0";
display();
}
public void openb(View view) {
char1 +="(";
display();
}
public void closeb(View view) {
char1+=")";
display();
}
public void calc(View view) {
TextView quantityTextViewdel = (TextView) findViewById(R.id.res);
temp=quantityTextViewdel.getText().toString();
if (temp.compareTo(t)==0)
{
char1="";
display();
}
else
{
//test code
j=0;
tempch=char1.toCharArray();
len=char1.length();
for(i=0;i<len;i++)
{
if (Character.isDigit(tempch[i]))
{
ch1[j++] = tempch[i];
}
else if (Character.isWhitespace(tempch[i]))
{
}
else
{
ch1[j++]=' ';
ch1[j++]=tempch[i];
ch1[j++]=' ';
}
}
for (i=0;i<len;i++)
{
tempch[i]='\0';
}
char1=""+String.valueOf(ch1);
for (i=0;i<j;i++)
{
ch1[i]='\0';
}
result=evaluate(char1);
char1=""+result;
j=0;
display();
}
}
public void add(View view) {
char1+="+";
display();
}
public void sub(View view) {
char1+="-";
display();
}
public void mul(View view) {
char1+="*";
display();
}
public void div(View view) {
char1+="/";
display();
}
public void del(View view) {
TextView quantityTextViewdel = (TextView) findViewById(R.id.res);
temp=quantityTextViewdel.getText().toString();
if (temp.compareTo(t)==0)
{
char1="";
display();
}
else
{
char1=""+char1.substring(0,char1.length()-1);
display();
}
}
public void clear(View view) {
char1="";
display();
}
/**
* This method displays the given quantity value on the screen.
*/
private void display() {
TextView quantityTextView = (TextView) findViewById(R.id.res);
quantityTextView.setText(char1);
}
private int evaluate(String expression)
{
char[] tokens = expression.toCharArray();
// Stack for numbers: 'values'
Stack<Integer> values = new Stack<Integer>();
// Stack for Operators: 'ops'
Stack<Character> ops = new Stack<Character>();
for (int i = 0; i < tokens.length; i++)
{
// Current token is a whitespace, skip it
if (tokens[i] == ' ')
continue;
// Current token is a number, push it to stack for numbers
if (tokens[i] >= '0' && tokens[i] <= '9')
{
StringBuilder sbuf = new StringBuilder();
// There may be more than one digits in number
while (i < tokens.length && tokens[i] >= '0' && tokens[i] <='9')
sbuf.append(tokens[i++]);
values.push(Integer.parseInt(sbuf.toString()));
}
// Current token is an opening brace, push it to 'ops'
else if (tokens[i] == '(')
ops.push(tokens[i]);
// Closing brace encountered, solve entire brace
else if (tokens[i] == ')')
{
while (ops.peek() != '(')
values.push(applyOp(ops.pop(), values.pop(), values.pop()));
ops.pop();
}
// Current token is an operator.
else if (tokens[i] == '+' || tokens[i] == '-' ||
tokens[i] == '*' || tokens[i] == '/')
{
// While top of 'ops' has same or greater precedence to current
// token, which is an operator. Apply operator on top of 'ops'
// to top two elements in values stack
while (!ops.empty() && hasPrecedence(tokens[i], ops.peek()))
values.push(applyOp(ops.pop(), values.pop(), values.pop()));
// Push current token to 'ops'.
ops.push(tokens[i]);
}
}
// Entire expression has been parsed at this point, apply remaining
// ops to remaining values
try {
while (!ops.empty())
values.push(applyOp(ops.pop(), values.pop(), values.pop()));
}
catch(EmptyStackException e1)
{
return 0;
}
// Top of 'values' contains result, return it
return values.pop();
}
// Returns true if 'op2' has higher or same precedence as 'op1',
// otherwise returns false.
private boolean hasPrecedence(char op1, char op2)
{
if (op2 == '(' || op2 == ')')
return false;
if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
return false;
else
return true;
}
// A utility method to apply an operator 'op' on operands 'a'
// and 'b'. Return the result.
private int applyOp(char op, int b, int a)
{
switch (op)
{
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
if (b == 0)
throw new
UnsupportedOperationException("Cannotdividebyzero");
return a / b;
}
return 0;
}
}
RES /布局/ activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/res"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:hint="value"
android:scrollHorizontally="true"
android:text=""
android:textSize="60sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="one"
android:text="1"
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="two"
android:text="2"
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="three"
android:text="3"
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="add"
android:text="+"
android:textSize="35sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="four"
android:text="4"
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="five"
android:text="5"
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="six"
android:text="6"
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="sub"
android:text="-"
android:textSize="35sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="seven"
android:text="7"
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="eight"
android:text="8"
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="nine"
android:text="9"
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="mul"
android:text="*"
android:textSize="35sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="horizontal"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="3"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="clear"
android:text="clr"
android:textSize="30sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="zero"
android:text="0"
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="del"
android:text="del"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="openb"
android:text="("
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="div"
android:text="/"
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="closeb"
android:text=")"
android:textSize="35sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="calc"
android:text="="
android:textSize="35sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
RES /布局脊/ activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/res"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:hint="value"
android:scrollHorizontally="true"
android:text=""
android:textSize="60sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="one"
android:text="1"
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="two"
android:text="2"
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="three"
android:text="3"
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="four"
android:text="4"
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="sub"
android:text="-"
android:textSize="35sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="five"
android:text="5"
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="six"
android:text="6"
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="seven"
android:text="7"
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="eight"
android:text="8"
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="add"
android:text="+"
android:textSize="35sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="horizontal"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="4"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="nine"
android:text="9"
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="zero"
android:text="0"
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="mul"
android:text="*"
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="div"
android:text="/"
android:textSize="35sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="clear"
android:text="clr"
android:textSize="30sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="del"
android:text="del"
android:textSize="30sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="openb"
android:text="("
android:textSize="35sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="closeb"
android:text=")"
android:textSize="35sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="calc"
android:text="="
android:textSize="35sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
我在res文件夹中安排布局和布局 - 土地文件夹的方式
RES /
布局/
activity_main.xml中
布局土地\
activity_main.xml中
minsdk是11
targetsdk是21
logcat文件
06-24 11:09:38.059 32152-32152/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.mabhi_000.myapplication, PID: 32152
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mabhi_000.myapplication/com.example.mabhi_000.myapplication.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
at com.example.mabhi_000.myapplication.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:515)atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="@android:style/Theme.Holo">
</style>
</resources>
我有什么问题可以告诉我吗?
答案 0 :(得分:0)
在res / values-folder中打开styles.xml文件并编辑行
<style name="AppTheme" parent="@android:style/Theme.Holo">
到
<style name="AppTheme" parent="Theme.AppCompat">
父属性的其他可能性是
Theme.AppCompat
Theme.AppCompat.Light
Theme.AppCompat.Light.DarkActionBar
Theme.AppCompat.NoActionBar
Theme.AppCompat.Light.NoActionBar