Java 致命异常:主Android Eclipse
我是android新手,正在尝试开发简单的计算器,但有一个致命的例外:main出现了,请帮助 我确实对我的onClickListner进行了评论以进行检查,但这一点都没有帮助Java 致命异常:主Android Eclipse,java,android,eclipse,exception,runtime,Java,Android,Eclipse,Exception,Runtime,我是android新手,正在尝试开发简单的计算器,但有一个致命的例外:main出现了,请帮助 我确实对我的onClickListner进行了评论以进行检查,但这一点都没有帮助 package com.example.calculatorsinglescreen; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.v
package com.example.calculatorsinglescreen;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
EditText value1,value2,myoperator,result;
Button ok;
String strvalue1,strvalue2,stroperator,strresult;
int ivalue1,ivalue2,ioperator,iresult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
value1 = (EditText) findViewById(R.id.txtvalue1);
value2 = (EditText) findViewById(R.id.txtvalue2);
myoperator = (EditText) findViewById(R.id.txtoperator);
result = (EditText) findViewById(R.id.txtresult);
ok = (Button) findViewById(R.id.btnok);
strvalue1 = value1.getText().toString();
strvalue2 = value2.getText().toString();
stroperator = myoperator.getText().toString();
ivalue1 = Integer.parseInt(strvalue1);
ivalue2 = Integer.parseInt(strvalue2);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (stroperator.equals("+")) {
Toast msg = Toast.makeText(MainActivity.this, "If is running", Toast.LENGTH_LONG);
msg.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.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的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.calculatorsinglescreen.MainActivity" >
<TextView
android:id="@+id/value1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="20dp"
android:text="Value 1" />
<TextView
android:id="@+id/value2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/value1"
android:layout_margin="20dp"
android:text="Value 2" />
<TextView
android:id="@+id/operator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/value2"
android:layout_margin="20dp"
android:text="Operator" />
<EditText
android:id="@+id/txtvalue1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/value1"
android:layout_alignBaseline="@+id/value1"
android:layout_alignParentRight="true"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/txtoperator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/operator"
android:layout_below="@+id/txtvalue2"
android:layout_toRightOf="@+id/operator"
android:layout_alignParentRight="true"
android:ems="10" />
<EditText
android:id="@+id/txtvalue2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/value2"
android:layout_toRightOf="@+id/value2"
android:layout_below="@+id/txtvalue1"
android:layout_alignParentRight="true"
android:ems="10" />
<Button
android:id="@+id/btnok"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Ok" />
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btnok"
android:layout_margin="40dp"
android:text="Result" />
<EditText
android:id="@+id/txtresult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/result"
android:layout_toRightOf="@+id/result"
android:ems="10" />
删除
ioperator=Integer.parseInt(stroperator)代码>来自代码。我认为您正在尝试将运算符转换为整数
ioperator = Integer.parseInt(stroperator);
如果您的意思是像这样的运算符+,-,/,*,则只能转换为thr char或保留字符串并执行如下操作:
private int plus(int ivalue1 , int ivalue2 , String operator ){
if(operator.equal("+")){
return ivalue1 + ivalue2;
}
return 0;
}
这就是错误所在
java.lang.ClassCastException: android.widget.TextView
检查第24行,看看是否在java-(xml)中转换了正确的小部件,这个错误意味着不兼容的转换小部件lol
如果一切看起来都是正确的-(意味着小部件在java中被强制转换为与它们各自的类一起),那么清除重建并重新启动
编辑:成功了。。你已经解决了,但是有一个新的错误
java.lang.NumberFormatException:
这是因为这些线
ivalue1 = Integer.parseInt(strvalue1);
ivalue2 = Integer.parseInt(strvalue2);
ioperator = Integer.parseInt(stroperator);
strvalue1 = value1.getText().toString();
把这些换成这个
Integer.valueOf(strvalue1); do that as follows
还要看看你的密码。。您的字符串值在oncreate期间从edittext中拉取,这意味着当应用程序在Onresume之前在oncreate中启动时(当应用程序显示在上时调用),在oncreate期间,用户无法与您的应用程序调情,并且无法进一步输入值,因此在最后从edittext中拉取时,字符串为空,所以基本上我说的是去掉这些线
ivalue1 = Integer.parseInt(strvalue1);
ivalue2 = Integer.parseInt(strvalue2);
ioperator = Integer.parseInt(stroperator);
strvalue1 = value1.getText().toString();
然后把它们放在点击事件中。。。我对你足够清楚了
EDIT2:总体代码
对于按钮单击
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
strvalue1 = value1.getText().toString();
strvalue2 = value2.getText().toString();
stroperator = myoperator.getText().toString();
ivalue1 = Integer.valueOf(strvalue1);
ivalue2 = Integer.valueOf(strvalue2);
if (stroperator.equals("+")) {
Toast.makeText(MainActivity.this, "If is running", Toast.LENGTH_LONG).show();
}
}
});
Stacktrace(logcat)请问…ioperator的值是多少?@jyoon logcat添加在这里java.lang.NumberFormatException:无法在第3行将“”解析为整数。@jyoon您能提供解决方案吗?问题是活动尚未启动OK,请以更多代码的形式提供。所有的活动,你们可以隐藏,若你们想要一些重要的东西或改变类的名称…是的,只是被我的命名惯例弄糊涂了,并纠正了它,但仍然是致命的例外!清理重建并重新启动..@HelpingDesk并让我们知道它是否因相同的错误崩溃。未工作LogCat现在已更新。请再次检查@Elltz是否意味着我应该将我的按钮侦听器代码粘贴到OnResume中?或者我应该在OnClick方法中粘贴我的字符串行?不!!这意味着您应该将value1.getText().toString()和其他内容粘贴到按钮单击侦听器中@帮助台