清沐_1412
2015-09-11 13:03
采纳率: 50%
浏览 4.5k

自学Android开发 在模拟器运行闪退

初学者自写的程序 ,但是在模拟器下运行闪退!!该怎么改呀??

package com.example.work3;

import java.util.ArrayList;
import java.util.List;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends TabActivity {
private TabHost tabhost;
private ListView listview;
private ArrayAdapter arr_adapter;
private Spinner spinner;
private Listlist;
private ArrayAdapter adapter;
private EditText et1,et2;
private Button bt;
String name;
String value;
String style;
@SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
et1=(EditText) findViewById(R.id.editText1);
et2=(EditText) findViewById(R.id.editText2);
bt=(Button) findViewById(R.id.button1);
setContentView(R.layout.activity_main);
tabhost=getTabHost();
tabhost.addTab(tabhost.newTabSpec("菜单一").setIndicator("首页").setContent(R.id.tab1));
tabhost.addTab(tabhost.newTabSpec("菜单二").setIndicator("新增").setContent(R.id.tab2));
tabhost.addTab(tabhost.newTabSpec("菜单三").setIndicator("账单").setContent(R.id.tab3));
//按钮监听 提取信息

    bt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
             name = et1.getText().toString();
             value = et2.getText().toString();
            style = spinner.getSelectedItem().toString();
        }
    });
    listview = (ListView) findViewById(R.id.listview);
    //新建适配器
    String[]arr_data={"数据1","数据2","数据3","数据4","数据5"};
    //ArrayAdapter(上下文,当前ListView加载的每一个列表项所对应的布局文件,数据源)
    arr_adapter =new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arr_data);
    listview.setAdapter(arr_adapter);
    spinner=(Spinner) findViewById(R.id.spinner);
    //数据源
    String[]list = {"1.日常食品支出","2.人情世故支出","3.出差旅游支出","4.服饰鞋帽支出","5.生活用品支出","6.其他支出"};
    //新建适配器

    adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
    //设置下拉样式
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    //加载适配器
    spinner.setAdapter(adapter);
}

//退出对话框
 @Override  
    public boolean onKeyDown(int keyCode, KeyEvent event)  
    {  
        if (keyCode == KeyEvent.KEYCODE_BACK )  
        {  
            // 创建退出对话框  
            AlertDialog isExit = new AlertDialog.Builder(this).create();  
            // 设置对话框标题  
            isExit.setTitle("系统提示");  
            // 设置对话框消息 

            isExit.setMessage("确定要退出吗");  
            // 添加选择按钮并注册监听  
            isExit.setButton("确定", listener);  
            isExit.setButton2("取消", listener);  
            // 显示对话框  
            isExit.show();  

        }  

        return false;  

    }  
    /**监听对话框里面的button点击事件*/  
    DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener()  
    {  
        public void onClick(DialogInterface dialog, int which)  
        {  
            switch (which)  
            {  
            case AlertDialog.BUTTON_POSITIVE:// "确认"按钮退出程序  
                finish();  
                break;  
            case AlertDialog.BUTTON_NEGATIVE:// "取消"第二个按钮取消对话框  
                break;  
            default:  
                break;  
            }  
        }  
    };    

}

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • 盼汕 2015-09-12 01:23
    已采纳

    你以后应该把错误log也贴出来,这样看可能会太累。不过幸好错误点很清晰:
    et1=(EditText) findViewById(R.id.editText1);
    et2=(EditText) findViewById(R.id.editText2);
    bt=(Button) findViewById(R.id.button1);
    setContentView(R.layout.activity_main);

    你报的是空指针错误,et1、et2、bt都赋的是null,原因是你的布局文件还没传进去解析。应该改成这样,setContentView要早于findviewby
    setContentView(R.layout.activity_main);
    et1=(EditText) findViewById(R.id.editText1);
    et2=(EditText) findViewById(R.id.editText2);
    bt=(Button) findViewById(R.id.button1);

    打赏 评论
  • bdmh 2015-09-11 13:36

    模拟器闪退还是你的app出错

    打赏 评论
  • 月盡天明 2015-09-12 02:05
     et1=(EditText) findViewById(R.id.editText1);
    et2=(EditText) findViewById(R.id.editText2);
    bt=(Button) findViewById(R.id.button1);
    setContentView(R.layout.activity_main);
    

    这段代码有问题。应该吧**setContentView放在findViewById的前面**。

    打赏 评论

相关推荐 更多相似问题