iT邦幫忙

2

求助,加了OnItemSelectedListener()事件,導致Android studio程式停止回應

發生錯誤的頁面

package com.skypan.school;

import ...


import static com.skypan.school.MainActivity.s_id;

public class repair extends AppCompatActivity {
    public static TextView Last;
    public static TextView tim;
    String times;
    String dateString;
    String timeString;
    String url="http://192...........";
    ListView LV1;//在activity_main為Listview命名的id
    String str = s_id.getText().toString().trim();//輸入帳號
    protected Spinner et2;
    protected Spinner et3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repair);
        initView();
        initDo();

        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads()
                .detectDiskWrites()
                .detectNetwork()
                .penaltyLog()
                .build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedClosableObjects()
                .penaltyLog()
                .penaltyDeath()
                .build());
        String r = DBstring.DB1("select * from text1");
        TextView TV=(TextView) findViewById(R.id.Last);
        TV.setText(r.toString());//剩餘數量顯示
 

        LV1 = (ListView) findViewById(R.id.LV1);//列表
        et2 =findViewById(R.id.et2);//下拉式選單et2
        et3 =findViewById(R.id.et3);//下拉式選單et3
        day = findViewById(R.id.days);
        //設定et2的選項
        String Morning ="早中";
        String Afternoon = "下午";
        String[] lunch ={Morning , Afternoon};
        ArrayAdapter<String> lunchList2 = new ArrayAdapter<>(repair.this,
                android.R.layout.simple_spinner_dropdown_item,
                lunch);et2.setAdapter(lunchList2);
                
        //新增資料:
        Button b1 = (Button) findViewById(R.id.button);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[] et0= new String[]{str,timeString,dateString};
                Inphp.Interting(et0,url);             
            }
        });
    }
    protected TextView day;//顯示選擇的星期
    private void initView(){
        et3 =findViewById(R.id.et3);
        day = findViewById(R.id.days);
        //設定時間格式
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sl = new SimpleDateFormat("u");
        //星期選項設定
        ArrayList<String> arrayas = new ArrayList<>();//
        int day99 = 3;//三個選項
        for(int i =0 ;i < day99 ;i++ ) {
            Calendar c99 = Calendar.getInstance();
            c99.add(Calendar.DAY_OF_MONTH,i);//加1天

            String datt99 = sl.format(c99.getTime());//當天星期幾
            int intV99 = Integer.valueOf(datt99);//intV = datt整數
            if(intV99 ==6 || intV99 ==7 )//選項不含六日
            {
                day99++;
            }else{
                SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd");
                ss.format(c99.getTime());
                String ddd = ss.format(c99.getTime());
                arrayas.add(ddd);
            }
        }
ArrayAdapter<String> lunchList = new ArrayAdapter<>(repair.this,android.R.layout.simple_spinner_dropdown_item,
 arrayas);et3.setAdapter(lunchList);
 //et3下拉式選單,選項放arrayas陣列的元素
    }
    private void initDo(){
        et3.setOnItemSelectedListener(new 
        AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                dateString = et3.getSelectedItem().toString().trim();
                day.setText(dateString);//設置選取選項到Textview see
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        et2.setOnItemSelectedListener(new 
        AdapterView.OnItemSelectedListener() {
            @Override
           public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                times = et2.getSelectedItem().toString().trim();
                tim.setText(times);
                if(times == "早上")
                {
                    timeString = "1";
                }else{
                    timeString = "2";
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }
}

Logcat顯示執行後發生錯誤的部分
https://ithelp.ithome.com.tw/upload/images/20200325/20125352MmZNUnpCqu.jpg
43行
String str = s_id.getText().toString().trim();
https://ithelp.ithome.com.tw/upload/images/20200325/20125352DraXH9Kokp.jpg
將43行的str刪除,並將有關的項目修改去除後Logcat顯示的錯誤
55和155的initDo();
https://ithelp.ithome.com.tw/upload/images/20200325/20125352xAM086u0GY.jpghttps://ithelp.ithome.com.tw/upload/images/20200325/2012535288aGDx9pyy.jpghttps://ithelp.ithome.com.tw/upload/images/20200325/201253520XA7lxzKkm.jpg
原先initDo();只有設定et3選取選項之後的部分
後面多加了et2選取選項後選項的部分 才開始發生錯誤

initDo()的部分

 private void initDo(){
        et3.setOnItemSelectedListener(new 
        AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                dateString = et3.getSelectedItem().toString().trim();
                day.setText(dateString);//設置選取選項到Textview see
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        et2.setOnItemSelectedListener(new 
        AdapterView.OnItemSelectedListener() {
            @Override
           public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                times = et2.getSelectedItem().toString().trim();
                tim.setText(times);
                if(times == "早上")
                {
                    timeString = "1";
                }else{
                    timeString = "2";
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

initDo()裡et2的語法刪除,程式就不會停止回應
有再想是否是因為et2的語法不正確導致運行會出錯(例如加入了if else的判斷式)
於是另外建了一個簡單的專案設了一樣的initDo()設定et3與et2的選取選項之後
但並沒有出現錯誤..............
https://ithelp.ithome.com.tw/upload/images/20200325/20125352IsuLMVN9XD.jpg

原先是想設定三個Spinner以及OnItemSelectedListener()事件........
不知道為何當前專案在initDo()設定et2被選取之後的動作會停止回應
嘗試去除et3的語法只留et2也是同理停止回應........
一開始Logcat顯示錯誤的Str字串反倒不受影響.......

1 個回答

0
dannyball710
iT邦新手 5 級 ‧ 2020-03-25 23:26:19

因為initDo();執行後才設定et2的內容,所以et2在上呼叫setOnItemSelectedListener時是et2是null

repair物件被建立時版面還沒建立,版面建立後才會呼叫onCreate,你寫在這裡會導致new repair()時就執行s_id.getText().toString().trim();導致s_id是null


當java出現錯誤時看這幾個地方可以解決大部分的問題,
java.lang.nullpointerexception是當你對null做操作時會出現的錯誤,底下會顯示哪一行出錯,錯誤一定就是那一行,針對那一行做處理就好,可以好好的學一下android物件的生命週期,要抓android裡面的東西大部分時候都要在onCreate之後才能抓。

Mangma iT邦新手 5 級 ‧ 2020-03-26 00:30:58 檢舉

之前也是常常寫完測試就是停止回應.....然後只會一個一個刪除的找,google也不知道問題,只能一直重試到成功,原來這部分和生命週期有關,謝謝大大詳細的解答,我會在去了解

我要發表回答

立即登入回答