无法让简单的Android计算器应用程序运行

时间:2012-10-26 03:43:54

标签: java android

我花了一些时间浏览一些谷歌的Android教程,并决定将我自己的简单计算器应用程序放在一起。我知道很多JAVA,并且在几个SDK之前已经搞砸了一些Android。我认为代码很好,但我无法让这个混蛋运行。我一直试图根据我的LogCat找到答案,但没有运气。首先,这是我的MainActivity.java:

package com.AppliedArgonautics.calculator;

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

public class MainActivity extends Activity {
    EditText editText1 = (EditText)findViewById(R.id.editText1);
    EditText editText2 = (EditText)findViewById(R.id.editText2);
    TextView textView1 = (TextView)findViewById(R.id.textView1);

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
public void Add(){
    float num1 = Float.parseFloat(editText1.getText().toString());
    float num2 = Float.parseFloat(editText2.getText().toString());
    float result = num1 + num2;
    textView1.setText(Float.toString(result));
}
public void Subtract(){
    float num1 = Float.parseFloat(editText1.getText().toString());
    float num2 = Float.parseFloat(editText2.getText().toString());
    float result = num1 - num2;
    textView1.setText(Float.toString(result));
}
public void Multiply(){
    float num1 = Float.parseFloat(editText1.getText().toString());
    float num2 = Float.parseFloat(editText2.getText().toString());
    float result = num1 * num2;
    textView1.setText(Float.toString(result));
}
public void Divide(){
    float num1 = Float.parseFloat(editText1.getText().toString());
    float num2 = Float.parseFloat(editText2.getText().toString());
    float result = num1 / num2;
    textView1.setText(Float.toString(result));
}
public void Clear(){
    editText1.setText("");
    editText2.setText("");
    textView1.setText("");
}
}

我的清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.AppliedArgonautics.calculator"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

然后,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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="32dp"
        android:onClick="Add"
        android:text="Add" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="45dp"
        android:onClick="Multiply"
        android:text="Multiply" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button3"
        android:layout_alignParentRight="true"
        android:layout_marginRight="28dp"
        android:onClick="Subtract"
        android:text="Subtract" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button3"
        android:layout_alignBottom="@+id/button3"
        android:layout_alignLeft="@+id/button2"
        android:onClick="Divide"
        android:text="Divide" />

     <Button
         android:id="@+id/button5"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@+id/button1"
         android:layout_centerHorizontal="true"
         android:onClick="Clear"
         android:text="Clear" />

     <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/button1"
        android:layout_marginTop="37dp"
        android:ems="10"
        android:inputType="phone" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editText1"
        android:layout_alignLeft="@+id/button2"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/editText1"
        android:ems="10"
        android:inputType="phone" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button5"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="56dp"
        android:text="TextView" />

</RelativeLayout>

这是我尝试运行时LogCat显示的内容:

/Trace   ( 3186): error opening trace file: No such file or directory (2)
D/AndroidRuntime( 3186): Shutting down VM
W/dalvikvm( 3186): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
E/AndroidRuntime( 3186): FATAL EXCEPTION: main
E/AndroidRuntime( 3186): java.lang.RuntimeException: Unable to instantiate activity           ComponentInfo{com.AppliedArgonautics.calculator/com.AppliedArgonautics.calculator.MainActivi    ty}: java.lang.NullPointerException
E/AndroidRuntime( 3186):    at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
E/AndroidRuntime( 3186):    at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
E/AndroidRuntime( 3186):    at     android.app.ActivityThread.access$600(ActivityThread.java:130)
E/AndroidRuntime( 3186):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
E/AndroidRuntime( 3186):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 3186):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 3186):    at android.app.ActivityThread.main(ActivityThread.java:4745)
E/AndroidRuntime( 3186):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 3186):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 3186):    at  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
E/AndroidRuntime( 3186):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime( 3186):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 3186): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 3186):    at android.app.Activity.findViewById(Activity.java:1825)
E/AndroidRuntime( 3186):    at com.AppliedArgonautics.calculator.MainActivity.<init>(MainActivity.java:10)
E/AndroidRuntime( 3186):    at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime( 3186):    at java.lang.Class.newInstance(Class.java:1319)
E/AndroidRuntime( 3186):    at  android.app.Instrumentation.newActivity(Instrumentation.java:1053)
E/AndroidRuntime( 3186):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
E/AndroidRuntime( 3186):    ... 11 more
W/ActivityManager(  160):   Force finishing activity com.AppliedArgonautics.calculator/.MainActivity

如果有人能看一眼我真的很感激。我认为学习android会很酷,但是我无法让这个简单的程序无法启动而感到非常沮丧。谢谢。

4 个答案:

答案 0 :(得分:3)

您的代码中可能存在很多问题,但首先是为了让您入门:

创建EditTextTextView作为实例变量。在onCreate

中为这些变量分配视图
public class MainActivity extends Activity {
        EditText editText1;
        EditText editText2;
        TextView textView1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     editText1 = (EditText)findViewById(R.id.editText1);
     editText2 = (EditText)findViewById(R.id.editText2);
     textView1 = (TextView)findViewById(R.id.textView1);
    }
.........
}

答案 1 :(得分:1)

我加载了你的应用程序,并以调试模式运行了应用程序。它打破了这条线

EditText editText1 = (EditText)findViewById(R.id.editText1);

它抛出了一个空指针异常,因为你还没有加载你的布局。这在OnCreate函数中发生。 解决方案是在SetContentView命令之后将findViewById命令转换为onCreate函数。即:

public class MainActivity extends Activity {
    EditText editText1;
    EditText editText2;
    TextView textView1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText1 = (EditText)findViewById(R.id.editText1);
    editText2 = (EditText)findViewById(R.id.editText2);
    textView1 = (TextView)findViewById(R.id.textView1);
}

您的下一个问题是您的Add,Subtract等功能都需要采用View类型的参数。这是返回单击按钮的引用。 (否则当找不到要调用的函数时会出现IllegalStateException)

然后它会在Float.parseFloat(editText1.getText()。toString())上失败,因为editText包含空格而不是数字。您要么强制编辑文本始终具有有效数字,要么修改您的函数以满足空字符串。

祝你好运! :)

答案 2 :(得分:1)

我在这里添加了值..你在xml中创建了按钮,但是没有在java代码中使用.. 我们需要在按钮点击监听器下完成我们的工作...

public class MainActivity extends Activity {
    EditText editText1 ;
    EditText editText2 ;
    TextView textView1 ;
    float num1, num2,result;
    Button add;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText1 = (EditText)findViewById(R.id.editText1);
    editText2 = (EditText)findViewById(R.id.editText2);
    textView1 = (TextView)findViewById(R.id.textView1);
    add = (Button)findViewById(R.id.button1);
    add.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             num1 = Float.parseFloat(editText1.getText().toString());
             num2 = Float.parseFloat(editText2.getText().toString());
             result = num1 + num2;
            textView1.setText(Float.toString(result));
        }
    });
}


}

对所有方法应用相同的东西......

希望,这会对你有帮助..

答案 3 :(得分:0)

写下这些

EditText editText1; 
EditText editText2;
TextView textView1 ;

而不是这些

EditText editText1 = (EditText)findViewById(R.id.editText1);
EditText editText2 = (EditText)findViewById(R.id.editText2);
TextView textView1 = (TextView)findViewById(R.id.textView1);

并更改此

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

@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 editText1 = (EditText)findViewById(R.id.editText1);
 editText2 = (EditText)findViewById(R.id.editText2);
 textView1 = (TextView)findViewById(R.id.textView1);

}

您收到错误是因为您尝试访问视图而未指定正在考虑的布局。

 setContentView(R.layout.activity_main);

指定您正在考虑的布局