android settextcolor错误:mysql调用期间切换屏幕

dbf7pr2w  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(372)

这个问题在这里已经有答案了

什么是nullpointerexception,如何修复它(12个答案)
两年前关门了。
正常情况下,它工作正常。但是,当您快速按下按钮并在0.1秒内切换屏幕时,屏幕可能会关闭。在db中加载字符时切换屏幕似乎发生了错误(我使用androidstudio-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.

问题摘要:
正常工作正常(安卓-php-mysql)
但是,如果我在0.1秒内按下另一个按钮来切换屏幕,则在从db加载值之前,屏幕会发生更改,并且关闭。
如何解决此问题?

ubby3x7f

ubby3x7f1#

你的 context 对象为空,这就是为什么 NullPointerException 它可能尚未初始化。
以及,下面

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

在上述声明中, getResources().getColor() 是不赞成的,而不是那样,使用如下-

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

相关问题