2 augussmile augussmile 于 2016.04.09 20:01 提问

Android手机应用程序闪退

自己是Android初学新手,按照课本上的示例敲代码,结果安装程序成功后,打开其闪退。。。问了好多次度娘,还是没有着落,还请各位前辈帮帮小弟,不胜感激。。。
图片说明

6个回答

augussmile
augussmile   2016.04.09 20:02

代码如下,还请各位前辈帮帮我。。。
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);
}

}

crazy1235
crazy1235   Ds   Rxr 2016.04.10 14:56

遇到错误先要看错误日志~~~
把你的错误日志贴出来看看。

a5911188
a5911188   2016.04.11 10:57

同学,你是这布局文件有问题,你贴这个代码没用的,肯定是你的XML文件里有错误,因为我看你贴出的异常提在setContent()的时候就被抛出了

XF1106
XF1106   2016.04.11 10:59

从一小部分错误日志猜测可能是布局问题,但是你错误日志贴的太不行了,关键错误只有一小部分可见;请帖全

u012215068
u012215068   2016.04.11 14:49

错误日志,要贴出报Exetion那段,和CauseBy那段,人家才能帮助到你!

chen_zhang_yu
chen_zhang_yu   2016.04.12 12:12

你把日志往右拉,看右半部分,你哪个com.example.myfirstapplication包下面哪个activity 26行,报错了。。应该是布局的问题。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!