gpt4 book ai didi

java - 简单计算器应用程序中的资源$NotFoundException

转载 作者:行者123 更新时间:2023-12-01 14:04:43 26 4
gpt4 key购买 nike

我刚刚学习了 Android 编码,这是一个简单的除法、乘法、加法和减法应用程序的源代码,该应用程序在我和我 friend 的 Android 手机上崩溃(在 Lenovo A390 和 Nexus 4 上测试)。下面是 MainActivity.java 代码,供您编译应用程序时使用。

package com.mayukhnair.mathmagic;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initializing buttons, fields etc.
final TextView resLabel = (TextView) findViewById(R.id.textView3);
final EditText num1 = (EditText) findViewById(R.id.editText1);
final EditText num2 = (EditText) findViewById(R.id.editText2);
Button getAddAnswerButton = (Button) findViewById(R.id.button1);
Button getSubtractButton = (Button) findViewById(R.id.button2);
Button getDivideButton = (Button) findViewById (R.id.button3);
Button getMultiplyButton = (Button) findViewById (R.id.button4);

// Multiply button functionality
getMultiplyButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
int n1=Integer.parseInt(num1.getText().toString());
int n2=Integer.parseInt(num2.getText().toString());
resLabel.setText((int) (n1*n2));

}
});

// Divide button functionality
getDivideButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
int n1=Integer.parseInt(num1.getText().toString());
int n2=Integer.parseInt(num2.getText().toString());
resLabel.setText((int) (n1/n2));

}
});

// Subtract button functionality
getSubtractButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
int n1=Integer.parseInt(num1.getText().toString());
int n2=Integer.parseInt(num2.getText().toString());
resLabel.setText((int) (n1-n2));
}
});

// Addition button functionality
getAddAnswerButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
int n1=Integer.parseInt(num1.getText().toString());
int n2=Integer.parseInt(num2.getText().toString());
resLabel.setText((int) (n1+n2));
}
});
}



@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;
}

}

现在,这是 Activity_main XML 文件,它将设计添加到应用程序中:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Number 1:" />

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:ems="10"
android:inputType="number" >

<requestFocus />
</EditText>

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="15dp"
android:text="Enter Number 2:" />

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:ems="10"
android:inputType="number" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_alignRight="@+id/textView3"
android:layout_below="@+id/button1"
android:layout_marginRight="36dp"
android:text="Subtract" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_alignRight="@+id/button2"
android:layout_below="@+id/editText2"
android:layout_marginTop="24dp"
android:text="Add" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_alignRight="@+id/button2"
android:layout_below="@+id/button2"
android:text="Divide" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView3"
android:layout_alignLeft="@+id/textView3"
android:layout_alignRight="@+id/button3"
android:text="Multiply" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_alignParentBottom="true"
android:layout_marginBottom="32dp"
android:text="This will be replaced with result."
android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

好吧,发生的情况是:Eclipse 不会给出任何错误,并且该应用程序会在我的手机上打开。我输入 2 个数字,然后按 4 个按钮中的任意一个(加、乘、除、减)。当我按下按钮时,应用程序崩溃并显示“不幸的是,MathMagic 已停止。”我疯狂地寻找错误。

因为我听说 LogCat 条目有很大帮助,所以给你(用 ** 标记的错误,用 * 标记的橙色警告):

09-25 16:45:18.861: D/ActivityThread(24302): BIND_APPLICATION handled : 0 /     AppBindData{appInfo=ApplicationInfo{4166a240 com.mayukhnair.mathmagic}}
09-25 16:45:18.872: D/jdwp(24302): processIncoming
09-25 16:45:18.881: D/jdwp(24302): processIncoming
09-25 16:45:18.881: D/jdwp(24302): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x400000ED, flags=0x0, dataLen=0x8
09-25 16:45:18.881: D/jdwp(24302): REQ: DDM.Chunk (cmd=199/1 dataLen=8 id=0x400000ed)
09-25 16:45:18.881: D/jdwp(24302): reply: dataLen=9 err=NONE(0)
09-25 16:45:18.888: D/jdwp(24302): processIncoming
09-25 16:45:18.888: D/jdwp(24302): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x17, id=0x400000EE, flags=0x0, dataLen=0xC
09-25 16:45:18.888: D/jdwp(24302): REQ: DDM.Chunk (cmd=199/1 dataLen=12 id=0x400000ee)
09-25 16:45:18.888: D/jdwp(24302): reply: dataLen=98 err=NONE(0)
09-25 16:45:18.888: D/jdwp(24302): processIncoming
09-25 16:45:18.888: D/jdwp(24302): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x400000EF, flags=0x0, dataLen=0x8
09-25 16:45:18.888: D/jdwp(24302): REQ: DDM.Chunk (cmd=199/1 dataLen=8 id=0x400000ef)
09-25 16:45:18.889: D/jdwp(24302): reply: dataLen=216 err=NONE(0)
09-25 16:45:18.889: D/jdwp(24302): processIncoming
09-25 16:45:18.889: D/jdwp(24302): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x400000F0, flags=0x0, dataLen=0x8
09-25 16:45:18.889: D/jdwp(24302): REQ: DDM.Chunk (cmd=199/1 dataLen=8 id=0x400000f0)
09-25 16:45:18.889: D/jdwp(24302): reply: dataLen=9 err=NONE(0)
09-25 16:45:18.991: D/ActivityThread(24302): ACT-AM_ON_RESUME_CALLED ActivityRecord{4166b780 token=android.os.BinderProxy@4166af40 {com.mayukhnair.mathmagic/com.mayukhnair.mathmagic.MainActivity}}
09-25 16:45:18.998: D/ActivityThread(24302): ACT-LAUNCH_ACTIVITY handled : 0 / ActivityRecord{4166b780 token=android.os.BinderProxy@4166af40 {com.mayukhnair.mathmagic/com.mayukhnair.mathmagic.MainActivity}}
09-25 16:45:18.999: D/ActivityThread(24302): ACT-AM_ON_PAUSE_CALLED ActivityRecord{4166b780 token=android.os.BinderProxy@4166af40 {com.mayukhnair.mathmagic/com.mayukhnair.mathmagic.MainActivity}}
09-25 16:45:19.002: D/ActivityThread(24302): ACT-PAUSE_ACTIVITY handled : 0 / android.os.BinderProxy@4166af40
09-25 16:45:19.004: V/Provider/Setting(24302): invalidate [system]: current 14 != cached 0
09-25 16:45:19.056: D/libEGL(24302): loaded /system/lib/egl/libGLES_android.so
09-25 16:45:19.063: D/libEGL(24302): loaded /vendor/lib/egl/libEGL_mtk.so
09-25 16:45:19.078: D/libEGL(24302): loaded /vendor/lib/egl/libGLESv1_CM_mtk.so
09-25 16:45:19.088: D/libEGL(24302): loaded /vendor/lib/egl/libGLESv2_mtk.so
09-25 16:45:19.127: D/OpenGLRenderer(24302): Enabling debug mode 0
09-25 16:45:19.131: D/ActivityThread(24302): ACT-STOP_ACTIVITY_SHOW handled : 0 / android.os.BinderProxy@4166af40
09-25 16:45:21.581: D/ActivityThread(24302): ACT-AM_ON_RESUME_CALLED ActivityRecord{4166b780 token=android.os.BinderProxy@4166af40 {com.mayukhnair.mathmagic/com.mayukhnair.mathmagic.MainActivity}}
09-25 16:45:21.581: D/ActivityThread(24302): ACT-RESUME_ACTIVITY handled : 0 / android.os.BinderProxy@4166af40
*09-25 16:45:28.368: W/ResourceType(24302): No package identifier when getting value for resource number 0x0000003c*
09-25 16:45:28.369: D/AndroidRuntime(24302): Shutting down VM
*09-25 16:45:28.369: W/dalvikvm(24302): threadid=1: thread exiting with uncaught exception (group=0x40d5f258)*
**09-25 16:45:28.389: E/AndroidRuntime(24302): FATAL EXCEPTION: main
09-25 16:45:28.389: E/AndroidRuntime(24302): android.content.res.Resources$NotFoundException: String resource ID #0x3c
09-25 16:45:28.389: E/AndroidRuntime(24302): at android.content.res.Resources.getText(Resources.java:253)
09-25 16:45:28.389: E/AndroidRuntime(24302): at android.widget.TextView.setText(TextView.java:3495)
09-25 16:45:28.389: E/AndroidRuntime(24302): at com.mayukhnair.mathmagic.MainActivity$4.onClick(MainActivity.java:69)
09-25 16:45:28.389: E/AndroidRuntime(24302): at android.view.View.performClick(View.java:3517)
09-25 16:45:28.389: E/AndroidRuntime(24302): at android.view.View$PerformClick.run(View.java:14155)
09-25 16:45:28.389: E/AndroidRuntime(24302): at android.os.Handler.handleCallback(Handler.java:605)
09-25 16:45:28.389: E/AndroidRuntime(24302): at android.os.Handler.dispatchMessage(Handler.java:92)
09-25 16:45:28.389: E/AndroidRuntime(24302): at android.os.Looper.loop(Looper.java:154)
09-25 16:45:28.389: E/AndroidRuntime(24302): at android.app.ActivityThread.main(ActivityThread.java:4624)
09-25 16:45:28.389: E/AndroidRuntime(24302): at java.lang.reflect.Method.invokeNative(Native Method)
09-25 16:45:28.389: E/AndroidRuntime(24302): at java.lang.reflect.Method.invoke(Method.java:511)
09-25 16:45:28.389: E/AndroidRuntime(24302): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
09-25 16:45:28.389: E/AndroidRuntime(24302): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
09-25 16:45:28.389: E/AndroidRuntime(24302): at dalvik.system.NativeStart.main(Native Method)**

所以,就是这样。我怀疑错误存在于 XML 设计文件中,但我找不到。帮助将不胜感激。谢谢。

最佳答案

在 TextView 上调用 set text 时,并且您希望将文本设置为字符串,而不是对 string.xml 中的行的引用,则必须确保您放入的类型是 String。

编辑所有 onClick 方法以在末尾添加 + ""。这会将 int 转换为字符串。示例:

resLabel.setText(((int) (n1-n2)) + "");

希望这能解决您的问题:)

关于java - 简单计算器应用程序中的资源$NotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19003668/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com