2013-09-25 66 views
0

我拿起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 [email protected] {com.mayukhnair.mathmagic/com.mayukhnair.mathmagic.MainActivity}} 
09-25 16:45:18.998: D/ActivityThread(24302): ACT-LAUNCH_ACTIVITY handled : 0/ActivityRecord{4166b780 [email protected] {com.mayukhnair.mathmagic/com.mayukhnair.mathmagic.MainActivity}} 
09-25 16:45:18.999: D/ActivityThread(24302): ACT-AM_ON_PAUSE_CALLED ActivityRecord{4166b780 [email protected] {com.mayukhnair.mathmagic/com.mayukhnair.mathmagic.MainActivity}} 
09-25 16:45:19.002: D/ActivityThread(24302): ACT-PAUSE_ACTIVITY handled : 0/[email protected] 
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/[email protected] 
09-25 16:45:21.581: D/ActivityThread(24302): ACT-AM_ON_RESUME_CALLED ActivityRecord{4166b780 [email protected] {com.mayukhnair.mathmagic/com.mayukhnair.mathmagic.MainActivity}} 
09-25 16:45:21.581: D/ActivityThread(24302): ACT-RESUME_ACTIVITY handled : 0/[email protected] 
*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

我已经编辑您的标题,使之更加有意义,并开门见山(“陷​​入困境”是不是为今后的研究,其中“资源$ NotFoundException”是一个显著的关键字)随时恢复到以前的状态如果你更喜欢。无论如何,不​​要使用表情符号。他们在这里不需要。 –

回答

1

当调用一个TextView集文本,要在文本设置为一个字符串,而不是在string.xml线的参考,你必须确保你在把类型有一个字符串。

编辑所有你的onClick方法追加一个+“”进行到底。这将把int转换为一个字符串。例如:

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

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

+0

如果我认识你,我会把你当成啤酒或可乐。消除了所有困难!荣誉和thnak你! –

+0

高兴我设法帮助:) –

+1

可以保持啤酒虽然...只是不要忘记标记为正确答案/给予好评^ - ^欢呼:d –

0

能否请你突出的java线没有。 69所以错误会更加清楚。

晴的错误是在

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

只能设置一个字符串变量的标签。请检查。

+0

请参阅吉尔Moshayof的评论上面。他的把戏做到了。 –

+0

你应该把它标记为一个答案,让人们知道这个问题就解决了。 –

相关问题