1

我花了一点时间浏览了一些 Google 的 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

4 回答 4

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);
    }
.........
}
于 2012-10-26T03:47:02.397 回答
1

我加载了您的应用程序,并在调试模式下运行该应用程序。断线了

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

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

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 包含空白,而不是数字。您需要强制编辑文本始终具有有效数字,或者修改您的函数以适应空字符串。

从这里祝你好运!:)

于 2012-10-26T04:00:17.903 回答
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));
        }
    });
}


}

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

希望,这会帮助你..

于 2012-10-26T04:07:50.983 回答
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);

指定您正在考虑的布局

于 2012-10-26T03:57:06.607 回答