我做了一个基本的android程序作为写作练习。但是无论何时打开它都会崩溃。我在手机上尝试过,但遗憾的是我的android工作室模拟器不能在我的电脑上工作,所以我的调试能力不是很多。它构建正常,所以我猜它不是语法错误或其他东西。
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.net.crackerjacksystems.talkies">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:theme="@style/AppTheme"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/Zero"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/Button0"
android:textSize="40sp"/>
<Button
android:id="@+id/One"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_above="@id/Zero"
android:text="@string/Button1"
android:textSize="40sp"/>
<Button
android:id="@+id/Two"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_above="@id/Zero"
android:layout_toRightOf="@+id/One"
android:layout_toEndOf="@+id/One"
android:text="@string/Button2"
android:textSize="40sp"/>
<Button
android:id="@+id/Three"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_above="@id/Zero"
android:layout_toRightOf="@+id/One"
android:layout_toEndOf="@+id/Two"
android:text="@string/Button3"
android:textSize="40sp"/>
<Button
android:id="@+id/Four"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_above="@+id/One"
android:text="@string/Button4"
android:textSize="40sp"/>
<Button
android:id="@+id/Five"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_above="@+id/One"
android:layout_toRightOf="@id/Four"
android:layout_toEndOf="@id/Four"
android:text="@string/Button5"
android:textSize="40sp"/>
<Button
android:id="@+id/Six"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_above="@+id/One"
android:layout_toRightOf="@id/Five"
android:layout_toEndOf="@id/Five"
android:text="@string/Button6"
android:textSize="40sp"/>
<Button
android:id="@+id/Seven"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_above="@+id/Four"
android:text="@string/Button7"
android:textSize="40sp"/>
<Button
android:id="@+id/Eight"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_above="@+id/Five"
android:layout_toRightOf="@id/Seven"
android:layout_toEndOf="@id/Seven"
android:text="@string/Button8"
android:textSize="40sp"/>
<Button
android:id="@+id/Nine"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_above="@+id/Six"
android:layout_toRightOf="@id/Eight"
android:layout_toEndOf="@id/Eight"
android:text="@string/Button9"
android:textSize="40sp"/>
<Button
android:id="@+id/Add"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_above="@+id/Seven"
android:text="@string/ButtonAdd"
android:textSize="40sp"/>
<Button
android:id="@+id/Subtract"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_above="@+id/Eight"
android:layout_toRightOf="@id/Add"
android:layout_toEndOf="@id/Add"
android:text="@string/ButtonSubtract"
android:textSize="40sp"/>
<Button
android:id="@+id/Multiply"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_above="@+id/Eight"
android:layout_toRightOf="@id/Subtract"
android:layout_toEndOf="@id/Subtract"
android:text="@string/ButtonMultiply"
android:textSize="40sp"/>
<Button
android:id="@+id/Divide"
android:layout_width="98dp"
android:layout_height="wrap_content"
android:layout_above="@+id/Eight"
android:layout_toRightOf="@id/Multiply"
android:layout_toEndOf="@id/Multiply"
android:text="@string/ButtonDivide"
android:textSize="40sp"/>
<Button
android:id="@+id/Clear"
android:layout_width="192dp"
android:layout_height="wrap_content"
android:layout_above="@+id/Add"
android:text="@string/ButtonClear"
android:textSize="40sp"/>
<Button
android:id="@+id/AllClear"
android:layout_width="192dp"
android:layout_height="wrap_content"
android:layout_above="@+id/Add"
android:layout_toRightOf="@id/Clear"
android:layout_toEndOf="@id/Clear"
android:text="@string/ButtonAllClear"
android:textSize="40sp"/>
<Button
android:id="@+id/Equals"
android:layout_width="192dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/Zero"
android:layout_toEndOf="@id/Zero"
android:layout_alignParentBottom="true"
android:text="@string/ButtonEqual"
android:textSize="40sp"/>
<TextView
android:id="@+id/Screen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/Clear"
android:textSize="82sp"
/>
</RelativeLayout>
MainActivity.java:
package com.net.crackerjacksystems.talkies;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity {
int first = 0, second = 0;
boolean onFirst = true;
char op;
Button zero = (Button)findViewById(R.id.Zero);
Button one = (Button)findViewById(R.id.One);
Button two = (Button)findViewById(R.id.Two);
Button three = (Button)findViewById(R.id.Three);
Button four = (Button)findViewById(R.id.Four);
Button five = (Button)findViewById(R.id.Five);
Button six = (Button)findViewById(R.id.Six);
Button seven = (Button)findViewById(R.id.Seven);
Button eight = (Button)findViewById(R.id.Eight);
Button nine = (Button)findViewById(R.id.Nine);
Button add = (Button)findViewById(R.id.Add);
Button sub = (Button)findViewById(R.id.Subtract);
Button mul = (Button)findViewById(R.id.Multiply);
Button div = (Button)findViewById(R.id.Divide);
Button cl = (Button)findViewById(R.id.Clear);
Button acl = (Button)findViewById(R.id.AllClear);
Button eql = (Button)findViewById(R.id.Equals);
TextView screen = (TextView)findViewById(R.id.Screen);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main();
}
void main(){
screen.setText(first);
zero.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(onFirst){first = (first*10);screen.setText(first);}
else{second = (second*10);screen.setText(second);}
}
});
one.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(onFirst){first = (first*10)+1;screen.setText(first);}
else{second = (second*10);screen.setText(second);}
}
});
two.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(onFirst){first = (first*10)+2;screen.setText(first);}
else{second = (second*10);screen.setText(second);}
}
});
three.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(onFirst){first = (first*10)+3;screen.setText(first);}
else{second = (second*10);screen.setText(second);}
}
});
four.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(onFirst){first = (first*10)+4;screen.setText(first);}
else{second = (second*10);screen.setText(second);}
}
});
five.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(onFirst){first = (first*10)+5;screen.setText(first);}
else{second = (second*10);screen.setText(second);}
}
});
six.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(onFirst){first = (first*10)+6;screen.setText(first);}
else{second = (second*10);screen.setText(second);}
}
});
seven.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(onFirst){first = (first*10)+7;screen.setText(first);}
else{second = (second*10);screen.setText(second);}
}
});
eight.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(onFirst){first = (first*10)+8;screen.setText(first);}
else{second = (second*10);screen.setText(second);}
}
});
nine.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(onFirst){first = (first*10)+9;screen.setText(first);}
else{second = (second*10);screen.setText(second);}
}
});
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
screen.setText(null);
op = '+';
onFirst = false;
}
});
sub.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
screen.setText(null);
op = '-';
onFirst = false;
}
});
mul.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
screen.setText(null);
op = '*';
onFirst = false;
}
});
div.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
screen.setText(null);
op = '/';
onFirst = false;
}
});
cl.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
screen.setText(null);
if(onFirst){first = 0;}
else{second = 0;}
}
});
acl.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
screen.setText(null);
first = 0;
second = 0;
}
});
eql.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(op == '+'){screen.setText(first+second);}
if(op == '-'){screen.setText(first-second);}
if(op == '*'){screen.setText(String.valueOf(first*second));}
if(op == '/'){screen.setText(String.valueOf(first/second));}
}
});
}
}
的strings.xml:
<resources>
<string name="app_name">Calculator</string>
<string name="Button1">1</string>
<string name="Button2">2</string>
<string name="Button3">3</string>
<string name="Button4">4</string>
<string name="Button5">5</string>
<string name="Button6">6</string>
<string name="Button7">7</string>
<string name="Button8">8</string>
<string name="Button9">9</string>
<string name="Button0">0</string>
<string name="ButtonAdd">+</string>
<string name="ButtonSubtract">—</string>
<string name="ButtonMultiply">✕</string>
<string name="ButtonDivide">÷</string>
<string name="ButtonClear">C</string>
<string name="ButtonAllClear">AC</string>
<string name="ButtonEqual">=</string>
</resources>
只是为了澄清,它是一个非常简单的计算器。
如果有人可以帮助解决为什么会崩溃,那会很好。
答案 0 :(得分:1)
将findViewById()
所有来电转移到onCreate()
内,因为它无法找到&#34;&#34;在致电setContentView()
之前。
private Button zero, one, two, three, four, five, six, seven, eight, nine, add, sub, mul, div, cl, acl, eql;
private TextView screen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
zero = (Button)findViewById(R.id.Zero);
one = (Button)findViewById(R.id.One);
two = (Button)findViewById(R.id.Two);
three = (Button)findViewById(R.id.Three);
four = (Button)findViewById(R.id.Four);
five = (Button)findViewById(R.id.Five);
six = (Button)findViewById(R.id.Six);
seven = (Button)findViewById(R.id.Seven);
eight = (Button)findViewById(R.id.Eight);
nine = (Button)findViewById(R.id.Nine);
add = (Button)findViewById(R.id.Add);
sub = (Button)findViewById(R.id.Subtract);
mul = (Button)findViewById(R.id.Multiply);
div = (Button)findViewById(R.id.Divide);
cl = (Button)findViewById(R.id.Clear);
acl = (Button)findViewById(R.id.AllClear);
eql = (Button)findViewById(R.id.Equals);
screen = (TextView)findViewById(R.id.Screen);
main();
}