[英]Android - portrait and landscape orientations?
I am learning android development and I have made a calculator app for practice.我正在学习 android 开发,我制作了一个用于练习的计算器应用程序。 It works fine in portrait mode.
它在纵向模式下工作正常。 So I made a new xml file for landscape after the app is not even working in portrait mode.
因此,在应用程序甚至无法在纵向模式下工作后,我为横向创建了一个新的 xml 文件。 As soon as it starts, it crashes with the message "Unfortunately app has stopped working".
一旦启动,它就会崩溃并显示“不幸的是应用程序已停止工作”的消息。 Here is my code
这是我的代码
AndroidManifest.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主活动.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/layout/activity_main.xml res/layout/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/layout-land/activity_main.xml res/layout-land/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>
The way in which i have arranged layout and layout-land folders in res folder我在 res 文件夹中排列 layout 和 layout-land 文件夹的方式
res/资源/
layout/布局/
activity_main.xml活动_main.xml
layout-land\\布局土地\\
activity_main.xml活动_main.xml
minsdk is 11 minsdk 是 11
targetsdk is 21目标 dk 是 21
logcat file日志文件
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>
what am i doing wrong can anyone tell me?我做错了什么有人可以告诉我吗?
In your res/values-folder open the styles.xml file and edit the line在您的 res/values 文件夹中打开 style.xml 文件并编辑该行
<style name="AppTheme" parent="@android:style/Theme.Holo">
to到
<style name="AppTheme" parent="Theme.AppCompat">
other possibilities for the parent attribute are父属性的其他可能性是
Theme.AppCompat
Theme.AppCompat.Light
Theme.AppCompat.Light.DarkActionBar
Theme.AppCompat.NoActionBar
Theme.AppCompat.Light.NoActionBar
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.