我在我的应用程序中找不到问题; /

时间:2014-02-19 21:29:53

标签: android android-activity

我有大问题!我是新的Android开发人员,当我调试或运行我的应用程序时,会出现一个空白活动!仅此而已!我无法理解你能找到问题吗? 我没有经验,这件事真让我紧张,我该怎么办?

package com.example.calculator;

import android.R.id;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends Activity implements OnClickListener {

    Button button;
    Button button2;
    Button button3; 
    EditText text;
    EditText text2;

    double x,y;
    double z=0;

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

        button = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);

        text = (EditText) findViewById(R.id.text1) ;
        text2 = (EditText) findViewById(R.id.text2) ;

        button.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);

        x=Double.parseDouble(text.getText().toString());
        y=Double.parseDouble(text2.getText().toString());
    }
    public void onClick(View v) {
          switch(v.getId()){

          case R.id.button1: 
                 z=x+y;
               break;

          case R.id.button2: 
                 z=x-y;
               break;

          case R.id.button3: 
              z=x*y;
              break;
          }

          String total = "0";
          total = String.valueOf(z);
          AlertDialog alertDialog;
          alertDialog = new AlertDialog.Builder(this).create();
          alertDialog.setTitle("Your result");
          alertDialog.setMessage(total);
          alertDialog.show();

    }





}

我的布局看起来像

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:background="@style/AppTheme"
    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:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="23dp"
        android:text="@string/num1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="22dp"
        android:text="@string/num2"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button3"
        android:layout_alignTop="@+id/textView1"
        android:ems="10"
        android:inputType="number"
        android:text="0" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/text1"
        android:layout_alignTop="@+id/textView2"
        android:ems="10"
        android:inputType="number"
        android:text="0" />

    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/button1"
        android:text="@string/minus" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView2"
        android:layout_alignTop="@+id/button2"
        android:text="@string/add" />

    <Button
        android:id="@+id/button3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/button2"
        android:text="@string/multiply" />

</RelativeLayout>

3 个答案:

答案 0 :(得分:0)

如果您有多个活动,其中一个空白,您可能正在启动您的应用,该活动被设置为启动一个 我建议你查看你的AndroidManefist.xml文件

你应该看到这个带有主(启动)活动的意图过滤器

<intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

确保将其包含在清单

中的MainActivity声明中
<activity> ... here ... </activity>

如果这没有帮助尝试修复项目属性并清理项目 (修复:在eclipse中右键单击项目文件夹 - &gt; Android工具 - &gt;修复项目属性) (清洁:从菜单中单击“项目” - &gt;清除 - &gt;选择您的项目并单击确定)

希望这有帮助:)

答案 1 :(得分:0)

刚刚在代码中进行了此更改。

public void onClick(View v) 
        {
             x=Double.parseDouble(text.getText().toString());
             y=Double.parseDouble(text2.getText().toString());

            switch(v.getId())

            {

              case R.id.button1: 
                     z=x+y;
                   break;

              case R.id.button2: 
                     z=x-y;
                   break;

              case R.id.button3: 
                  z=x*y;
                  break;
              }

              String total = "0";
              total = String.valueOf(z);
              AlertDialog alertDialog;
              alertDialog = new AlertDialog.Builder(this).create();
              alertDialog.setTitle("Your result");
              alertDialog.setMessage(total);
              alertDialog.show();

        }

注意:确保你正在验证你的EditText,也就是你在edittext中输入一些数字,否则它会给你NumberFormatException bcoz你给点击解析。

答案 2 :(得分:0)

您的错误是由以下引起的:​​java.lang.NumberFormatException :,因此将字符串解析为double应该在action(onClick)中(解析null时出错):此代码完美运行:

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

public class MainActivity extends Activity implements android.view.View.OnClickListener {

    Button button;
    Button button2;
    Button button3; 
    EditText text;
    EditText text2;

    double x=0,y=0;
    double z=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);

        text = (EditText) findViewById(R.id.text1) ;
        text2 = (EditText) findViewById(R.id.text2) ;


        button.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);



    }
    public void onClick(View v) {
        //parsing should be here
        x=Double.parseDouble(text.getText().toString());
        y=Double.parseDouble(text2.getText().toString());
          switch(v.getId()){

          case R.id.button1: 
                 z=x+y;
               break;

          case R.id.button2: 
                 z=x-y;
               break;

          case R.id.button3: 
              z=x*y;
              break;
          }

          String total = "0";
          total = String.valueOf(z);
          AlertDialog alertDialog;
          alertDialog = new AlertDialog.Builder(this).create();
          alertDialog.setTitle("Your result");
          alertDialog.setMessage(total);
          alertDialog.show();

    }
}

enter image description here