我刚刚选择了Android编码,这是一个简单的分区,乘法,加法和减法应用程序的源代码,这个应用程序崩溃了我和我朋友的Android手机(在联想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设计文件中,但我无法找到。帮助将不胜感激。感谢。
答案 0 :(得分:1)
在TextView上调用set text时,如果要将文本设置为字符串,而不是对string.xml中的行进行引用,则必须确保要放入的类型为String。
编辑你所有的onClick方法,在末尾添加一个+“”。这会将int转换为字符串。例如:
resLabel.setText(((int) (n1-n2)) + "");
希望这能解决你的问题:)
答案 1 :(得分:0)
请您突出显示java行号。 69因此错误会更加明确。
大部分错误都在
中 resLabel.setText((int) (n1-n2));
您只能将String变量设置为标签。请检查一下。