2017-02-18 99 views
1

我是新的编码,我试图做一个简单的应用程序,从2变量进行减法,但是当我点击按钮来计算,应用程序崩溃...有人可以帮助我在代码中发现问题? (Android Studio中)问题与一个简单的应用

下面的代码:

package apps.ste.resto; 

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Button; 
import android.widget.TextView; 

import static apps.ste.resto.R.id.risultato; 

public class MainActivity extends AppCompatActivity { 

TextView importo; 
TextView conto; 
TextView risultato; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 

    TextView risultato = (TextView) findViewById(R.id.textView); 
    TextView importo = (TextView) findViewById(R.id.editText); 
    TextView conto = (TextView) findViewById(R.id.editText2); 
    Button bottone = (Button) findViewById(R.id.button); 
    bottone.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      result(); 
     } 
    }); 
} 

public void result(){ 
    float result = Float.parseFloat(importo.getText().toString()) - Float.parseFloat(conto.getText().toString()); 
    risultato.setText(Float.toString(result)); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
+0

你是从result()方法中调用result()方法,是不是会创建无限循环?另外,请发布logcat。 –

+0

你可以发布错误日志吗? –

+0

我无法发布错误日志,我不使用emuletor,因为速度很慢,只需要10米即可打开它:( –

回答

0

我认为你是对的getText()获取空值..

,所以你可以试试这个:

public void result(){ 

    boolean digitsOnly1 = TextUtils.isDigitsOnly(importo.getText()+""); 
    boolean digitsOnly2 = TextUtils.isDigitsOnly(conto.getText()+""); 

    if(digitsOnly1 && digitsOnly2){ 
     float result = Float.parseFloat(importo.getText().toString()) - Float.parseFloat(conto.getText().toString()); 
     risultato.setText(result+"")); 
    }else{ 
     risultato.setText("Please provide inputs"); 
    } 

}