【问题标题】:Android SetTextColor Error: Screen switching during mySQL call [duplicate]Android SetTextColor错误:mySQL调用期间的屏幕切换[重复]
【发布时间】:2018-01-19 08:03:32
【问题描述】:

正常情况下,它运行正常。 但是,当您快速按下按钮并在 0.1 秒内切换屏幕时,它可能会关闭。 在DB中加载字符时切换屏幕似乎发生了错误。 (我用的是android studio-php-mySQL)


快速换屏时,关闭点为setTextColor。

    // Since the timetable is up to 13th grade, i <14;
    for(int i = 0; i<14; i++)
    {
        // if the value in the current array is not empty
        if(!this.monday[i].equals(""))
        {
            // In this way, the contents of the array will be populated in a text view named 'monday'.
            monday[i].setText(this.monday[i]);

            // The color of the text changes when the lecture is present.
            monday[i].setTextColor(context.getResources().getColor(R.color.colorPrimaryDark));
        }

        else
        {
            // If it is blank, it adjusts the table size according to maxString.
            monday[i].setText(maxString);
        }
    }

而导致错误的代码一起是Android与DB连接的点。这个地方根本不会造成任何问题。 (如果你慢慢按下按钮)

class BackgroundTask extends AsyncTask<Void, Void, String>
{
    String target;  

    @Override
    protected void onPreExecute() {

        try
        {   
            target = "http://MyHomepageID.cafe24.com/ScheduleList.php?userID=" + URLEncoder.encode(MainActivity. userID, "UTF-8");  //해당 웹 서버에 접속
        }

        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    protected String doInBackground(Void... voids) {
        try {

            URL url = new URL(target);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();


            InputStream inputStream = httpURLConnection.getInputStream();


            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));


            String temp;
            StringBuilder stringBuilder = new StringBuilder();


            while ((temp=bufferedReader.readLine()) != null)
            {
                stringBuilder.append(temp + "\n");
            }


            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect(); 
            return stringBuilder.toString().trim();
        }

        catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void onProgressUpdate(Void... values) {
        super.onProgressUpdate();
    }

    @Override  
    public void onPostExecute(String result) {
        try {

            JSONObject jsonObject = new JSONObject(result);


            JSONArray jsonArray = jsonObject.getJSONArray("response");  

            int count = 0;
            String courseProfessor;
            String courseTime;
            String courseTitle;
            int courseID;



            while (count < jsonArray.length())
            {
                JSONObject object = jsonArray.getJSONObject(count);


                courseID = object.getInt("courseID");
                courseProfessor = object.getString("courseProfessor");
                courseTime = object.getString("courseTime");
                courseTitle = object.getString("courseTitle");  


                schedule.addSchedule(courseTime, courseTitle, courseProfessor);
                count++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        schedule.setting(monday, tuesday, wednesday, thursday, friday, getContext());
    }
}

上面列出了错误日志中显示为蓝色的两个代码。

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.aaaaa.registeration, PID: 12358
              java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
                  at com.example.aaaaa.registeration.Schedule.setting(Schedule.java:685)
                  at com.example.aaaaa.registeration.ScheduleFragment$BackgroundTask.onPostExecute(ScheduleFragment.java:258)
                  at com.example.aaaaa.registeration.ScheduleFragment$BackgroundTask.onPostExecute(ScheduleFragment.java:159)
                  at android.os.AsyncTask.finish(AsyncTask.java:667)
                  at android.os.AsyncTask.-wrap1(AsyncTask.java)
                  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:684)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Application terminated.

问题总结:

  1. 正常工作正常。 (android - php - mysql)

  2. 但是,如果我在 0.1 秒内通过按下另一个按钮来切换屏幕,则会在从 DB 加载值之前更改屏幕,然后将其关闭。

  3. 我该如何解决这个问题?

【问题讨论】:

    标签: android mysql android-studio-3.0 settext


    【解决方案1】:

    您的context 对象为空,这就是您得到NullPointerException 的原因,它可能尚未初始化。

    还有,下面

    monday[i].setTextColor(context.getResources().getColor(R.color.colorPrimaryDark);
    

    在上面的声明中,getResources().getColor() 已被弃用,而不是那样使用,如下所示 -

    monday[i].setTextColor(ContextCompat.getColor(this, R.color.color_orange_text));
    

    【讨论】:

    • 非常感谢您的回复。 [1] 星期一[i].setTextColor (ContextCompat.getColor (this, R.color.color_orange_text)); → 此时,当我输入“color_orange_text”时,出现了“无法解析符号'color_orange_text'”这句话。 [2] 星期一[i].setTextColor(ContextCompat.getColor(this, R.color.colorPrimaryDark)); → 所以,当场,我把之前创建的“colorPrimaryDark”放进去,这次在[this]里面加了一条红线。 [这] 说,“错误的第一个参数类型。找到:'com.example.aaaaa.registeration.Schedule',需要:'android.content.Context'”。
    • [3] 星期一[i].setTextColor(ContextCompat.getColor(context, R.color.colorPrimaryDark)); → [链接] stackoverflow.com/questions/35624743/… 所以让我们在谷歌上搜索一下,我发现有人使用“上下文”而不是“这个”。因此,当我将“上下文”放入其中时,一开始效果很好。然而,当我用手指切换到另一个场景几次时,同样的症状出现了,每秒四次。无论如何,感谢您抽出宝贵的时间来回答。如果正常使用,不会出错。这不是紧急问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多