嗨试着制作一个计算器,但在单个问题上从3天开始。当我点击等于按钮应用程序停止工作。阅读了很多帖子,但找不到解决方案,所以最后在这里询问。
Xml代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="@+id/one"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="156dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="@+id/two"
android:layout_alignTop="@+id/one"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="@+id/three"
android:layout_alignTop="@+id/two"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="@+id/four"
android:layout_centerVertical="true"
android:layout_alignRight="@+id/one"
android:layout_alignEnd="@+id/one" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:id="@+id/five"
android:layout_above="@+id/seven"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:id="@+id/six"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/three"
android:layout_alignStart="@+id/three" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:id="@+id/seven"
android:layout_marginTop="44dp"
android:layout_below="@+id/four"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:id="@+id/eight"
android:layout_alignTop="@+id/seven"
android:layout_alignRight="@+id/five"
android:layout_alignEnd="@+id/five"
android:layout_alignLeft="@+id/five"
android:layout_alignStart="@+id/five" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:id="@+id/nine"
android:layout_alignTop="@+id/eight"
android:layout_alignRight="@+id/six"
android:layout_alignEnd="@+id/six" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/zero"
android:layout_below="@+id/eight"
android:layout_centerHorizontal="true"
android:layout_marginTop="47dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:id="@+id/clear"
android:layout_alignBottom="@+id/zero"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:id="@+id/equals"
android:layout_alignTop="@+id/zero"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="@+id/add"
android:layout_marginBottom="29dp"
android:layout_above="@+id/two"
android:layout_alignRight="@+id/one"
android:layout_alignEnd="@+id/one" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sub"
android:text="-"
android:layout_alignTop="@+id/add"
android:layout_alignLeft="@+id/two"
android:layout_alignStart="@+id/two" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x"
android:id="@+id/mul"
android:layout_alignTop="@+id/sub"
android:layout_toLeftOf="@+id/three"
android:layout_toStartOf="@+id/three" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:id="@+id/div"
android:layout_alignTop="@+id/mul"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/out"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
package com.example.parth.calculator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
public class MainActivity extends AppCompatActivity {
public static int a,b,c,d,h,f,u,n,x;
public TextView car;
public static int digit() {
if ( a==0 && b == 0 && c == 0 && d == 0 ){
d = x ;
} else if (a==0 && b == 0 && c == 0 && d != 0 ){
c = d ;
d = x ;
} else if (a==0 && b == 0 && c != 0 && d != 0 ){
b = c;
c= d ;
d =x ;
}else if (a==0 && b != 0 && c != 0 && d != 0 ) {
a = b;
b = c;
c = d;
d = x;
}
// else not defined.
else{
d = 0;
}
n = 1000*a + 100*b + 10*c + d ;
return n;
}
//method digit end.
public static int func(){
if (f == 1 ){
h += n;
return h ;
}
else if(f == 2){
h -=n;
return h ;
}
else if(f == 3){
h *= n;
return h ;
}
else if(f == 4){
h /= n;
return h ;
}
else {
h = n;
return h ;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button one, two, three, four, five, six, seven, eight, nine, zero,
equals, add, sub, mul, div;
setContentView(R.layout.activity_main);
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);
zero = (Button) findViewById(R.id.zero);
equals = (Button) findViewById(R.id.equals);
add = (Button) findViewById(R.id.add);
sub = (Button) findViewById(R.id.sub);
mul = (Button) findViewById(R.id.mul);
div = (Button) findViewById(R.id.div);
car = (TextView) findViewById(R.id.out);
// Methods for click button start
add.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
f = 1;
}
}
);
sub.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
f = 2;
}
}
);
mul.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
f = 3;
}
}
);
div.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
f = 4;
}
}
);
one.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
x = 1;
digit();
func();
}
}
);
two.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
x = 2;
digit();
func();
}
}
);
three.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
x = 3;
digit();
func();
}
}
);
four.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
x = 4;
digit();
func();
}
}
);
five.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
x = 5;
digit();
func();
}
}
);
six.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
x = 6;
digit();
func();
}
}
);
seven.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
x = 7 ;
digit();
func();
}
}
);
eight.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
x = 8;
digit();
func();
}
}
);
nine.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
x = 9;
digit();
func();
}
}
);
zero.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
x= 0;
digit();
func();
}
}
);
// Methods for click button end
// Methods for function start
//extra
equals.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
car.setText(h);
}
}
);
}
的logcat&GT;
10-25 17:34:49.592 26980-26980/? I/SELinux: Function: selinux_android_load_priority , priority [2] , priority version is VE=SEPF_SM-E700H_4.4.4_A046
10-25 17:34:49.592 26980-26980/? E/SELinux: [DEBUG] get_category: variable seinfocat: default sensitivity: NULL, cateogry: NULL
10-25 17:34:49.592 26980-26980/? E/dalvikvm: >>>>> Normal User
10-25 17:34:49.592 26980-26980/? E/dalvikvm: >>>>> com.example.parth.calculator [ userId:0 | appId:10263 ]
10-25 17:34:49.592 26980-26980/? E/SELinux: [DEBUG] get_category: variable seinfocat: default sensitivity: NULL, cateogry: NULL
10-25 17:34:49.592 26980-26980/? I/libpersona: KNOX_SDCARD checking this for 10263
10-25 17:34:49.592 26980-26980/? I/libpersona: KNOX_SDCARD not a persona
10-25 17:34:49.602 26980-26980/? D/dalvikvm: Late-enabling CheckJNI
10-25 17:34:49.612 26980-26980/? D/TimaKeyStoreProvider: in addTimaSignatureService
10-25 17:34:49.612 26980-26980/? D/TimaKeyStoreProvider: Cannot add TimaSignature Service, License check Failed
10-25 17:34:49.612 26980-26980/? D/ActivityThread: Added TimaKesytore provider
10-25 17:34:49.702 26980-26980/? W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
10-25 17:34:49.702 26980-26980/? I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onSearchRequested
10-25 17:34:49.702 26980-26980/? W/dalvikvm: VFY: unable to resolve interface method 17896: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
10-25 17:34:49.702 26980-26980/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
10-25 17:34:49.702 26980-26980/? I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onWindowStartingActionMode
10-25 17:34:49.702 26980-26980/? W/dalvikvm: VFY: unable to resolve interface method 17900: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
10-25 17:34:49.702 26980-26980/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
10-25 17:34:49.732 26980-26980/? I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
10-25 17:34:49.732 26980-26980/? W/dalvikvm: VFY: unable to resolve virtual method 421: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
10-25 17:34:49.742 26980-26980/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
10-25 17:34:49.742 26980-26980/? I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
10-25 17:34:49.742 26980-26980/? W/dalvikvm: VFY: unable to resolve virtual method 443: Landroid/content/res/TypedArray;.getType (I)I
10-25 17:34:49.742 26980-26980/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
10-25 17:34:49.782 26980-26980/? D/Activity: #1 setTransGradationModeColor false
10-25 17:34:49.832 26980-26980/? I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: ()
10-25 17:34:49.832 26980-26980/? I/Adreno-EGL: OpenGL ES Shader Compiler Version: E031.24.02.07
10-25 17:34:49.832 26980-26980/? I/Adreno-EGL: Build Date: 11/29/14 Sat
10-25 17:34:49.832 26980-26980/? I/Adreno-EGL: Local Branch:
10-25 17:34:49.832 26980-26980/? I/Adreno-EGL: Remote Branch:
10-25 17:34:49.832 26980-26980/? I/Adreno-EGL: Local Patches:
10-25 17:34:49.832 26980-26980/? I/Adreno-EGL: Reconstruct Branch:
10-25 17:34:49.852 26980-26980/? D/OpenGLRenderer: Enabling debug mode 0
10-25 17:34:50.892 26980-26980/? D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
10-25 17:34:51.212 26980-26980/? D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
10-25 17:34:51.432 26980-26980/? D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
10-25 17:34:51.562 26980-26980/? W/ResourceType: No package identifier when getting value for resource number 0x00000001
10-25 17:34:51.562 26980-26980/? D/AndroidRuntime: Shutting down VM
10-25 17:34:51.562 26980-26980/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41bcbda0)
10-25 17:34:51.562 26980-26980/? E/AndroidRuntime: FATAL EXCEPTION: main
10-25 17:34:51.562 26980-26980/? E/AndroidRuntime: Process: com.example.parth.calculator, PID: 26980
10-25 17:34:51.562 26980-26980/? E/AndroidRuntime: android.content.res.Resources$NotFoundException: String resource ID #0x1
10-25 17:34:51.562 26980-26980/? E/AndroidRuntime: at android.content.res.Resources.getText(Resources.java:1407)
10-25 17:34:51.562 26980-26980/? E/AndroidRuntime: at android.widget.TextView.setText(TextView.java:5131)
10-25 17:34:51.562 26980-26980/? E/AndroidRuntime: at com.example.parth.calculator.MainActivity$5.onClick(MainActivity.java:219)
10-25 17:34:51.562 26980-26980/? E/AndroidRuntime: at android.view.View.performClick(View.java:4881)
10-25 17:34:51.562 26980-26980/? E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:19592)
10-25 17:34:51.562 26980-26980/? E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:733)
10-25 17:34:51.562 26980-26980/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-25 17:34:51.562 26980-26980/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:146)
10-25 17:34:51.562 26980-26980/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5756)
10-25 17:34:51.562 26980-26980/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
10-25 17:34:51.562 26980-26980/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
10-25 17:34:51.562 26980-26980/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
10-25 17:34:51.562 26980-26980/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
10-25 17:34:51.562 26980-26980/? E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
10-25 17:34:53.152 26980-26980/? I/Process: Sending signal. PID: 26980 SIG: 9
答案 0 :(得分:1)
android.content.res.Resources $ NotFoundException:字符串资源ID
由于以下行:
car.setText(h);
h
是int
,它不是int
中有效的资源ID.show TextView
:
car.setText(String.valueOf(h));