我希望用户单击两个按钮之一进行计算,但onClicklistiner and the public void
onClick`会导致应用崩溃。
但是在新的空白java主要工作中同样的事情。两组代码都放在下面。按钮实现代码是唯一的区别是第一组代码设置导航抽屉
package com.androidbelieve.tradingtechnicals;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.view.Menu;
import com.androidbelieve.drawerwithswipetabs.R;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdListener;
import java.text.NumberFormat;
public abstract class MainActivity extends AppCompatActivity implements View.OnClickListener {
DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mNavigationView = (NavigationView) findViewById(R.id.shitstuff);
mNavigationView.setItemIconTintList(null);
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView, new TabFragment()).commit();
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
mDrawerLayout.closeDrawers();
if (menuItem.getItemId() == R.id.nav_formations) {
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView, new fomations()).commit();
}
if (menuItem.getItemId() == R.id.nav_main_menu) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new TabFragment()).commit();
}
if (menuItem.getItemId() == R.id.candle_sticks_drawer) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new candlesticks()).commit();
}
if (menuItem.getItemId() == R.id.candle_sticks_drawer) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new candlesticks()).commit();
}
if (menuItem.getItemId() == R.id.nav_Stochastic) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new stochastic()).commit();
}
if (menuItem.getItemId() == R.id.nav_Relative_strength) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new Relative_Strength()).commit();
}
if (menuItem.getItemId() == R.id.nav_MACD) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new MACD()).commit();
}
if (menuItem.getItemId() == R.id.nav_moving_averages) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new moving_averages()).commit();
}
if (menuItem.getItemId() == R.id.nav_help) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new help()).commit();
}
if (menuItem.getItemId() == R.id.nav_additional_content) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new additional_content_1()).commit();
}
if (menuItem.getItemId() == R.id.nav_p_e) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new Fundemental_metrics()).commit();
}
if (menuItem.getItemId() == R.id.nav_cdf) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new eq_cfd()).commit();
}
return false;
}
});
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.app_name,
R.string.app_name);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
public void onClick(View v)
{
switch (v.getId()){
case R.id.button1:
EditText working_capital = (EditText) findViewById(R.id.working_capital);
EditText total_assets = (EditText) findViewById(R.id.total_assets);
EditText ebit = (EditText) findViewById(R.id.ebit);
EditText market_val_eq = (EditText) findViewById(R.id.market_value_of_equity);
EditText book_val_lib = (EditText) findViewById(R.id.book_lib);
EditText retained_ern = (EditText) findViewById(R.id.retained_earnings);
EditText sales = (EditText) findViewById(R.id.sales);
TextView answer = (TextView) findViewById(R.id.result);
TextView comment = (TextView) findViewById(R.id.comment);
if (working_capital.getText().toString().trim().length() == 0 || total_assets.getText().toString().trim().length() == 0 || ebit.getText().toString().trim().length() == 0 || market_val_eq.getText().toString().trim().length() == 0 || book_val_lib.getText().toString().trim().length() == 0 || retained_ern.getText().toString().trim().length() == 0 || sales.getText().toString().trim().length() == 0) {
answer.setText("Result");
} else {
double num1 = Double.parseDouble(working_capital.getText().toString());
double num2 = Double.parseDouble(total_assets.getText().toString());
double num3 = Double.parseDouble(ebit.getText().toString());
double num4 = Double.parseDouble(market_val_eq.getText().toString());
double num5 = Double.parseDouble(book_val_lib.getText().toString());
double num6 = Double.parseDouble(retained_ern.getText().toString());
double num7 = Double.parseDouble(sales.getText().toString());
double sum = 1.2 * (num1 / num2) + 1.4 * (num6 / num2) + 3.3 * (num3 / num2) + 0.6 * (num4 / num5) + (num7 / num2);
answer.setText(String.format("%.2f", sum));
if (sum >= 2.99) {
comment.setText("Successful");
} else if (sum <= 1.81) {
comment.setText("Unsuccessful");
} else {
comment.setText("Grey zone");
}
}
break;
case R.id.button2:
ebit = (EditText) findViewById(R.id.ebit);
EditText net_profit = (EditText) findViewById(R.id.net_profit);
EditText assets = (EditText) findViewById(R.id.assets);
EditText Sales = (EditText) findViewById(R.id.sales);
EditText Equity = (EditText) findViewById(R.id.equity);
TextView answer2 = (TextView) findViewById(R.id.result2);
if (assets.getText().toString().trim().length() == 0 || net_profit.getText().toString().trim().length() == 0 || Sales.getText().toString().trim().length() == 0 || Equity.getText().toString().trim().length() == 0) {
answer2.setText("Result");
} else {
double num8 = Double.parseDouble(net_profit.getText().toString());
double num9 = Double.parseDouble(assets.getText().toString());
double num10 = Double.parseDouble(ebit.getText().toString());
double num11 = Double.parseDouble(Equity.getText().toString());
double sum2 = (num8 / num10) * (num10 / num9) * (num9 / num11);
answer2.setText(String.format("%.2f", sum2));
}
}
}
}
package engineeringtrader.myapplication;
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.EditText;
import android.widget.Switch;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
button1.setOnClickListener(this);
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();
}
});
}
@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);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
EditText working_capital = (EditText) findViewById(R.id.working_capital);
EditText total_assets = (EditText) findViewById(R.id.total_assets);
EditText ebit = (EditText) findViewById(R.id.ebit);
EditText market_val_eq = (EditText) findViewById(R.id.market_value_of_equity);
EditText book_val_lib = (EditText) findViewById(R.id.book_lib);
EditText retained_ern = (EditText) findViewById(R.id.retained_earnings);
EditText sales = (EditText) findViewById(R.id.sales);
TextView answer = (TextView) findViewById(R.id.result);
TextView comment = (TextView) findViewById(R.id.comment);
if (working_capital.getText().toString().trim().length() == 0 || total_assets.getText().toString().trim().length() == 0 || ebit.getText().toString().trim().length() == 0 || market_val_eq.getText().toString().trim().length() == 0 || book_val_lib.getText().toString().trim().length() == 0 || retained_ern.getText().toString().trim().length() == 0 || sales.getText().toString().trim().length() == 0) {
answer.setText("Result");
} else {
double num1 = Double.parseDouble(working_capital.getText().toString());
double num2 = Double.parseDouble(total_assets.getText().toString());
double num3 = Double.parseDouble(ebit.getText().toString());
double num4 = Double.parseDouble(market_val_eq.getText().toString());
double num5 = Double.parseDouble(book_val_lib.getText().toString());
double num6 = Double.parseDouble(retained_ern.getText().toString());
double num7 = Double.parseDouble(sales.getText().toString());
double sum = 1.2 * (num1 / num2) + 1.4 * (num6 / num2) + 3.3 * (num3 / num2) + 0.6 * (num4 / num5) + (num7 / num2);
answer.setText(String.format("%.2f", sum));
if (sum >= 2.99) {
comment.setText("Successful");
} else if (sum <= 1.81) {
comment.setText("Unsuccessful");
} else {
comment.setText("Grey zone");
}
}
break;
case R.id.button2:
break;
}
}
}
答案 0 :(得分:0)
findViewById中的任何一个视图都返回null,因此抛出NullReferenceException或Double.parseDouble()之一正在接收一个空字符串,这将导致NumberFormatException。
因此,您必须检查您的视图是否为空,并且在解析之前检查视图是否包含可以解析的有效文本。