问题描述
解决方案
代码如下,还请各位前辈帮帮我。。。
package com.example.myfirstapplycation;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.view.View.OnClickListener;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
private TextView showView=null;
private TextView note=null;
private EditText editNum1=null;
private EditText editNum2=null;
private Button butadd=null;
private Button butsub=null;
private Button butmul=null;
private Button butdiv=null;
private int num1=0;
private int num2=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.showView=(TextView)super.findViewById(R.id.mytext);
this.editNum1=(EditText)super.findViewById(R.id.myeda);
this.editNum2=(EditText)super.findViewById(R.id.myedb);
this.butadd=(Button)super.findViewById(R.id.mybutadd);
this.butsub=(Button)super.findViewById(R.id.mybutsub);
this.butmul=(Button)super.findViewById(R.id.mybutmul);
this.butdiv=(Button)super.findViewById(R.id.mybutdiv);
this.note=(TextView)super.findViewById(R.id.note);
this.butadd.setOnClickListener(new AddListener());
this.butsub.setOnClickListener(new SubListener());
this.butmul.setOnClickListener(new MulListener());
this.butdiv.setOnClickListener(new DivListener());
this.editNum1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
MainActivity.this.editNum1.setText("");
}
});
this.editNum2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
MainActivity.this.editNum2.setText("");
}
});
}
private class AddListener implements OnClickListener{
public void onClick(View v){
MainActivity.this.num1=Integer.parseInt(MainActivity.this.editNum1.getText().toString());
MainActivity.this.num2=Integer.parseInt(MainActivity.this.editNum2.getText().toString());
MainActivity.this.note.setText("+");
MainActivity.this.showView.setText(String.valueOf(num1+num2));}
}
private class SubListener implements OnClickListener{
public void onClick(View v){
MainActivity.this.num1=Integer.parseInt(MainActivity.this.editNum1.getText().toString());
MainActivity.this.num2=Integer.parseInt(MainActivity.this.editNum2.getText().toString());
MainActivity.this.note.setText("-");
MainActivity.this.showView.setText(String.valueOf(num1-num2));}
}
private class MulListener implements OnClickListener{
public void onClick(View v){
MainActivity.this.num1=Integer.parseInt(MainActivity.this.editNum1.getText().toString());
MainActivity.this.num2=Integer.parseInt(MainActivity.this.editNum2.getText().toString());
MainActivity.this.note.setText("*");
MainActivity.this.showView.setText(String.valueOf(num1*num2));}
}
private class DivListener implements OnClickListener{
public void onClick(View v){
MainActivity.this.num1=Integer.parseInt(MainActivity.this.editNum1.getText().toString());
MainActivity.this.num2=Integer.parseInt(MainActivity.this.editNum2.getText().toString());
MainActivity.this.note.setText("/");
MainActivity.this.showView.setText(String.valueOf(num1/num2));}
}
@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文件里有错误,因为我看你贴出的异常提在setContent()的时候就被抛出了
解决方案四:
从一小部分错误日志猜测可能是布局问题,但是你错误日志贴的太不行了,关键错误只有一小部分可见;请帖全
解决方案五:
错误日志,要贴出报Exetion那段,和CauseBy那段,人家才能帮助到你!
解决方案六:
你把日志往右拉,看右半部分,你哪个com.example.myfirstapplication包下面哪个activity 26行,报错了。。应该是布局的问题。