首页 > 本机的android虚拟机跑不起android小项目

本机的android虚拟机跑不起android小项目

我是照着老师这个视频敲得代码的,但总是跑不出来。。
无图无真相

这是应该有的效果

但是本机的android虚拟机确实这样。。

附上我的源代码

main.java

package com.example.cal;

import android.R.id;
import android.R.integer;
import android.support.v7.app.ActionBarActivity;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity implements  OnClickListener {
    Button btn_0;//0数字按钮
    Button btn_1;//1数字按钮
    Button btn_2;//2数字按钮
    Button btn_3;//3数字按钮
    Button btn_4;//4数字按钮
    Button btn_5;//5数字按钮
    Button btn_6;//6数字按钮
    Button btn_7;//7数字按钮
    Button btn_8;//8数字按钮
    Button btn_9;//9数字按钮
    Button btn_point;//小数点数字按钮
    Button btn_clear;//清除按钮
    Button btn_delete;//删除按钮
    Button btn_plus;//加号按钮
    Button btn_minus;//减号按钮
    Button btn_del;//删除按钮
    Button btn_multiply;//乘号按钮
    Button btn_divide;//除号按钮
    Button btn_equal;//等号按钮
    EditText et_input;//显示输入内容的显示屏
    boolean clear_flag=false;//清空标识
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_0=(Button) findViewById(R.id.btn_0);
        btn_1=(Button) findViewById(R.id.btn_1);
        btn_2=(Button) findViewById(R.id.btn_2);
        btn_3=(Button) findViewById(R.id.btn_3);
        btn_4=(Button) findViewById(R.id.btn_4);
        btn_5=(Button) findViewById(R.id.btn_5);
        btn_6=(Button) findViewById(R.id.btn_6);
        btn_7=(Button) findViewById(R.id.btn_7);
        btn_8=(Button) findViewById(R.id.btn_8);
        btn_9=(Button) findViewById(R.id.btn_9);
        btn_del = (Button) findViewById(R.id.btn_del);
        btn_plus=(Button) findViewById(R.id.btn_plus);
        btn_multiply=(Button) findViewById(R.id.btn_multiply);
        btn_divide=(Button) findViewById(R.id.btn_divide);
        btn_equal=(Button) findViewById(R.id.btn_equal);
        btn_minus=(Button) findViewById(R.id.btn_minus);
        et_input=(EditText) findViewById(R.id.et_input);
        //以上是按钮初始化
        btn_0.setOnClickListener(this);
        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_3.setOnClickListener(this);
        btn_4.setOnClickListener(this);
        btn_5.setOnClickListener(this);
        btn_6.setOnClickListener(this);
        btn_7.setOnClickListener(this);
        btn_8.setOnClickListener(this);
        btn_9.setOnClickListener(this);
        btn_point.setOnClickListener(this);
        btn_clear.setOnClickListener(this);
        btn_del.setOnClickListener(this);
        btn_minus.setOnClickListener(this);
        btn_plus.setOnClickListener(this);
        btn_multiply.setOnClickListener(this);
        btn_divide.setOnClickListener(this);
        btn_equal.setOnClickListener(this);
        //以上是设置按钮的点击事件
    }

    @Override
    public void onClick(View v) {
        String str=et_input.getText().toString();
        switch (v.getId()) {
            case R.id.btn_0:
            case R.id.btn_1:
            case R.id.btn_2:
            case R.id.btn_3:
            case R.id.btn_4:
            case R.id.btn_5:
            case R.id.btn_6:
            case R.id.btn_7:
            case R.id.btn_8:
            case R.id.btn_9:
            case R.id.btn_point:
                if(clear_flag){
                    clear_flag=false;
                    str="";
                    et_input.setText("");
                }
                et_input.setText(str+((Button)v).getText());
                break;
            case R.id.btn_plus:
            case R.id.btn_minus:
            case R.id.btn_multiply:
            case R.id.btn_divide:
                if(clear_flag){
                    clear_flag=false;
                    str="";
                    et_input.setText("");
                }
                et_input.setText(str+" "+((Button)v).getText()+" ");
                break;
            case R.id.btn_del:
                if(clear_flag){
                    clear_flag=false;
                    str="";
                    et_input.setText("");
                }else if (str!=null&&!str.equals("")) {
                    et_input.setText(str.substring(0,str.length()-1));
                }
                break;
            case R.id.btn_clear:
                clear_flag=false;
                str="";
                et_input.setText("");
                break;
            case R.id.btn_equal:
                getReault();
                break;
            default:
                break;
        }
    }
    /*
     * 运算结果
     * **/
    private void getReault() {
        String exp=et_input.getText().toString();
        if (exp.equals("")||exp==null) {
            return;
        }
        if (!exp.contains(" ")) {
            return;
        }
        if(clear_flag){
            clear_flag=false;
        }
        clear_flag=true;
        double res=0.0;
        String s1=exp.substring(0,exp.indexOf(" "));//截取运算符前面的字符串
        String op=exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);//截取运算符
        String s2=exp.substring(exp.indexOf(" ")+3);//截取运算符后面的字符串
        if (!s1.equals("")&&!s2.equals("")) {
            double d1=Double.parseDouble(s1);
            double d2=Double.parseDouble(s2);
            if (op.equals("+")) {
                res=d1+d2;
            }else if(op.equals("-")){
                res=d1-d2;
            }else if(op.equals("*")){
                res=d1*d2;
            }else if(op.equals("/")){
                if(d2==0)   res=0;
                else res=d1/d2;
            }
            if (!s1.contains(".")&&!s2.contains(".")) {
                int r=(int)res;
                et_input.setText(r+"");
            }else {
                et_input.setText(res+"");
            }
        }
        else if(!s1.equals("")&&s2.equals(" ")){//5 + 
            et_input.setText(res+"");
        }else if(s1.equals("")&&!s2.equals("")){
            double d2=Double.parseDouble(s2);
            if (op.equals("+")) {
                res=d2;
            }else if(op.equals("-")){
                res=-d2;
            }else{
                res=0;
            }
            if (!s1.contains(".")&&!s2.contains(".")) {
                int r=(int)res;
                et_input.setText(r+"");
            }else {
                et_input.setText(res+"");
            }
        }else {
            et_input.setText("");
        }
    }
}

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="com.example.cal.MainActivity" >
    <EditText
        android:id="@+id/et_input"
        android:layout_width="fill_parent"
        android:layout_height="60dip"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:background="@drawable/white_bg"
        android:editable="false"
        android:ems="10"
        android:gravity="right|bottom"
        android:text="50" >
        <requestFocus />
    </EditText>
    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/linearLayout1"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="90dp"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_clear"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:text="C"
            android:background="@drawable/white_select"
            android:textSize="20sp" />

        <Button
            android:id="@+id/btn_0"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:text="0"
            android:background="@drawable/white_select"
            android:textSize="20sp" />

        <Button
            android:id="@+id/btn_point"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:text="."
            android:background="@drawable/white_select"
            android:textSize="20sp" />
        <Button
            android:id="@+id/btn_divide"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:text="/"
            android:background="@drawable/white_select"
            android:textSize="20sp" />

    </LinearLayout>



     <LinearLayout
         android:id="@+id/linearLayout2"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_above="@+id/linearLayout3"
         android:layout_alignLeft="@+id/linearLayout3"
         android:gravity="center"
         android:orientation="horizontal" >

         <Button
             android:id="@+id/btn_3"
             android:layout_width="60dp"
             android:layout_height="60dp"
             android:background="@drawable/white_select"
             android:text="3"
             android:textSize="20sp" />

         <Button
             android:id="@+id/btn_2"
             android:layout_width="60dp"
             android:layout_height="60dp"
             android:background="@drawable/white_select"
             android:text="2"
             android:textSize="20sp" />

         <Button
             android:id="@+id/btn_1"
             android:layout_width="60dp"
             android:layout_height="60dp"
             android:background="@drawable/white_select"
             android:text="1"
             android:textSize="20sp" />

         <Button
             android:id="@+id/btn_multiply"
             android:layout_width="60dp"
             android:layout_height="60dp"
             android:background="@drawable/white_select"
             android:text="X"
             android:textSize="20sp" />
     </LinearLayout>

     <LinearLayout
         android:id="@+id/linearLayout1"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_above="@+id/linearLayout2"
         android:layout_alignLeft="@+id/et_input"
         android:gravity="center"
         android:orientation="horizontal" >

         <Button
             android:id="@+id/btn_6"
             android:layout_width="60dp"
             android:layout_height="60dp"
             android:background="@drawable/white_select"
             android:text="6"
             android:textSize="20sp" />

         <Button
             android:id="@+id/btn_5"
             android:layout_width="60dp"
             android:layout_height="60dp"
             android:background="@drawable/white_select"
             android:text="5"
             android:textSize="20sp" />

         <Button
             android:id="@+id/btn_4"
             android:layout_width="60dp"
             android:layout_height="60dp"
             android:background="@drawable/white_select"
             android:text="4"
             android:textSize="20sp" />

         <Button
             android:id="@+id/btn_minus"
             android:layout_width="60dp"
             android:layout_height="60dp"
             android:background="@drawable/white_select"
             android:text="—"
             android:textSize="20sp" />
     </LinearLayout>

     <LinearLayout
         android:id="@+id/linearLayout0"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_above="@+id/linearLayout1"
         android:layout_alignLeft="@+id/linearLayout1"
         android:gravity="center"
         android:orientation="horizontal" >

         <Button
             android:id="@+id/btn_9"
             android:layout_width="60dp"
             android:layout_height="60dp"
             android:background="@drawable/white_select"
             android:text="9"
             android:textSize="20sp" />

         <Button
             android:id="@+id/btn_8"
             android:layout_width="60dp"
             android:layout_height="60dp"
             android:background="@drawable/white_select"
             android:text="8"
             android:textSize="20sp" />

         <Button
             android:id="@+id/btn_7"
             android:layout_width="60dp"
             android:layout_height="60dp"
             android:background="@drawable/white_select"
             android:text="7"
             android:textSize="20sp" />

         <Button
             android:id="@+id/btn_plus"
             android:layout_width="60dp"
             android:layout_height="60dp"
             android:background="@drawable/white_select"
             android:text="+"
             android:textSize="20sp" />
     </LinearLayout>

      <LinearLayout
          android:id="@+id/linearLayout4"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/linearLayout3"
          android:layout_alignParentBottom="true"
          android:gravity="center"
          android:orientation="horizontal" >

          <Button
              android:id="@+id/btn_del"
              android:layout_width="120dp"
              android:layout_height="60dp"
              android:background="@drawable/white_select"
              android:text="删除"
              android:textSize="20sp" />


          <Button
              android:id="@+id/btn_equal"
              android:layout_width="120dp"
              android:layout_height="60dp"
              android:background="@drawable/white_select"
              android:text="="
              android:textSize="20sp" />
      </LinearLayout>

</RelativeLayout>

btn_clearbtn_point这两个按钮没有初始化


这是我的日志


这算什么问题……跑不起来看问题看日志啊,贴代码有什么用?

空指针异常,一般就是你初始化控件的问题,你把edt_input的初始化放到所有btn之前试试

【热门文章】
【热门文章】