初學者自寫的程序 ,但是在模擬器下運行閃退!!該怎麼改呀??
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;
}
}
};
}
你以後應該把錯誤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);