Androidstudio报错

我现在有一个主布局,和几个小布局,这个软件通过socket通信接收数据,我想让接收到的数据显示在小布局上

当我在MainActivity.java里面声明并使用它时我发现软件出现了闪退,下面是我的错误信息

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.jiedianshuju, PID: 23746
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.example.jiedianshuju.MainActivity$8$1.run(MainActivity.java:200)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:742)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:632)

下面是我的小布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    tools:context=".yijieDian">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/text1"
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第一组"
            android:textSize="200sp">
        </TextView>
        <TextView
            android:id="@+id/text2"
            android:layout_below="@id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="节点状态:已连接"
            android:textSize="120sp">
        </TextView>
        <TextView
            android:id="@+id/text3"
            android:layout_below="@id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="连接数据:"
            android:textSize="120sp">
        </TextView>
        <TextView
            android:id="@+id/textLi4"
            android:layout_below="@id/text2"
            android:layout_toRightOf="@id/text3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textSize="120sp">
        </TextView>
    </RelativeLayout>

</LinearLayout>

以及我的主程序


package com.example.jiedianshuju;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class MainActivity extends AppCompatActivity {
    private ImageButton imageButton1, imageButton2, imageButton3, imageButton4, imageButton5, imageButton6;
    private Socket socket;
    private View view;
    private TextView textLi4,textView1, textView2, textView3, textView4, textView5, textView6, textView7, textView8, textView9, textView10, textView11, textView12;
    private Context context = this;

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

        socketAd();
        imageButton1 = findViewById(R.id.image1);
        imageButton2 = findViewById(R.id.image2);
        imageButton3 = findViewById(R.id.image3);
        imageButton4 = findViewById(R.id.image4);
        imageButton5 = findViewById(R.id.image5);
        imageButton6 = findViewById(R.id.image6);

        textView1 = findViewById(R.id.textView1);
        textView2 = findViewById(R.id.textView2);
        textView3 = findViewById(R.id.textView3);
        textView4 = findViewById(R.id.textView4);
        textView5 = findViewById(R.id.textView5);
        textView6 = findViewById(R.id.textView6);
        textView7 = findViewById(R.id.textView7);
        textView8 = findViewById(R.id.textView8);
        textView9 = findViewById(R.id.textView9);
        textView10 = findViewById(R.id.textView10);
        textView11 = findViewById(R.id.textView11);
        textView12 = findViewById(R.id.textView12);

        textLi4 = findViewById(R.id.textLi4);

        Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/dongman.ttf");
        textView1.setTypeface(typeface);
        textView2.setTypeface(typeface);
        textView3.setTypeface(typeface);
        textView4.setTypeface(typeface);
        textView5.setTypeface(typeface);
        textView6.setTypeface(typeface);
        textView7.setTypeface(typeface);
        textView8.setTypeface(typeface);
        textView9.setTypeface(typeface);
        textView10.setTypeface(typeface);
        textView11.setTypeface(typeface);
        textView12.setTypeface(typeface);

        TranslateAnimation animation = new TranslateAnimation(0, 0, 400, 0);
        animation.setRepeatCount(0);
        animation.setDuration(3000);

        imageButton1.setAnimation(animation);
        imageButton2.setAnimation(animation);
        imageButton3.setAnimation(animation);
        imageButton4.setAnimation(animation);
        imageButton5.setAnimation(animation);
        imageButton6.setAnimation(animation);

        imageButton1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                //Handler handler = new Handler();
                //handler.postDelayed(new Runnable() {
                //@Override
                //public void run() {
                //imageButton1.setImageResource(R.drawable.yello);
                //}
                //},1000);
                //imageButton1.setImageResource(R.drawable.blue);
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    Intent intent = new Intent();
                    intent.setClass(MainActivity.this, yijieDian.class);
                    startActivity(intent);
                }
                return false;

            }
        });
        imageButton2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    Intent intent = new Intent();
                    intent.setClass(MainActivity.this, erjieDian.class);
                    startActivity(intent);
                }
                return false;
            }
        });
        imageButton3.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    Intent intent = new Intent();
                    intent.setClass(MainActivity.this, sanjieDian.class);
                    startActivity(intent);
                }
                return false;
            }
        });
        imageButton4.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    Intent intent = new Intent();
                    intent.setClass(MainActivity.this, sijieDian.class);
                    startActivity(intent);
                }
                return false;
            }
        });
        imageButton5.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    Intent intent = new Intent();
                    intent.setClass(MainActivity.this, wujieDian.class);
                    startActivity(intent);
                }
                return false;
            }
        });
        imageButton6.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    Intent intent = new Intent();
                    intent.setClass(MainActivity.this, liujieDian.class);
                    startActivity(intent);
                }
                return false;
            }
        });
    }

    private void socketAd() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    socket = new Socket("172.18.1.15", 6003);
                    receiveCmd();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void receiveCmd() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {

                        // 读取数据
                        final byte[] buffer = new byte[1024];
                        int len = socket.getInputStream().read(buffer);
                        while (len != -1) {
                            // do something with buffer
                            len = socket.getInputStream().read(buffer);

                            if (buffer[0] == (byte) 0x55 && buffer[11] == (byte) 0xaa)//解析正确
                            {
                                if (buffer[2] == (byte) 0x01)//第一个节点
                                {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            float temp = 0, hum = 0, light = 0;
                                            temp = (float) ((buffer[3] << 8) | buffer[4]) / 100;
                                            hum = (float) ((buffer[5] << 8) | buffer[6]) / 100;
                                            light = (float) ((buffer[7] << 8) | buffer[8]) / 100;
                                            textView2.setText("已连接");
                                            textLi4.setText("温度: "+temp+"湿度:"+hum+"光照:"+light);
                                        }
                                    });
                                } else if (buffer[2] == (byte) 0x02)//第二个节点
                                {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            float temp = 0, hum = 0, light = 0;
                                            temp = (float) ((buffer[3] << 8) | buffer[4]) / 100;
                                            hum = (float) ((buffer[5] << 8) | buffer[6]) / 100;
                                            light = (float) ((buffer[7] << 8) | buffer[8]) / 100;
                                            textView4.setText("已连接");
                                            Toast.makeText(context, "温度:" + String.valueOf(temp) + "  湿度:" + String.valueOf(hum) + "  光照:" + String.valueOf(light), Toast.LENGTH_SHORT).show();
                                        }
                                    });

                                } else if (buffer[2] == (byte) 0x03)//第三个节点
                                {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            float temp = 0, hum = 0, light = 0;
                                            temp = (float) ((buffer[3] << 8) | buffer[4]) / 100;
                                            hum = (float) ((buffer[5] << 8) | buffer[6]) / 100;
                                            light = (float) ((buffer[7] << 8) | buffer[8]) / 100;
                                            textView6.setText("已连接");
                                            Toast.makeText(context, "温度:" + String.valueOf(temp) + "  湿度:" + String.valueOf(hum) + "  光照:" + String.valueOf(light), Toast.LENGTH_SHORT).show();
                                        }
                                    });

                                } else if (buffer[2] == (byte) 0x04)//第四个节点
                                {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(MainActivity.this, "节点四连接成功", Toast.LENGTH_LONG).show();
                                            float temp = 0, hum = 0, light = 0;
                                            temp = (float) ((buffer[3] << 8) | buffer[4]) / 100;
                                            hum = (float) ((buffer[5] << 8) | buffer[6]) / 100;
                                            light = (float) ((buffer[7] << 8) | buffer[8]) / 100;
                                            textView8.setText("已连接");
                                            Toast.makeText(context, "温度:" + String.valueOf(temp) + "  湿度:" + String.valueOf(hum) + "  光照:" + String.valueOf(light), Toast.LENGTH_SHORT).show();
                                        }
                                    });

                                } else if (buffer[2] == (byte) 0x05)//第五个节点
                                {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(MainActivity.this, "节点五连接成功", Toast.LENGTH_LONG).show();
                                            float temp = 0, hum = 0, light = 0;
                                            temp = (float) ((buffer[3] << 8) | buffer[4]) / 100;
                                            hum = (float) ((buffer[5] << 8) | buffer[6]) / 100;
                                            light = (float) ((buffer[7] << 8) | buffer[8]) / 100;
                                            textView10.setText("已连接");
                                            Toast.makeText(context, "温度:" + String.valueOf(temp) + "  湿度:" + String.valueOf(hum) + "  光照:" + String.valueOf(light), Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                } else if (buffer[2] == (byte) 0x06)//第六个节点
                                {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(MainActivity.this, "节点六连接成功", Toast.LENGTH_LONG).show();
                                            float temp = 0, hum = 0, light = 0;
                                            temp = (float) ((buffer[3] << 8) | buffer[4]) / 100;
                                            hum = (float) ((buffer[5] << 8) | buffer[6]) / 100;
                                            light = (float) ((buffer[7] << 8) | buffer[8]) / 100;
                                            textView12.setText("已连接");
                                            Toast.makeText(context, "温度:" + String.valueOf(temp) + "  湿度:" + String.valueOf(hum) + "  光照:" + String.valueOf(light), Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                }
                            }
                        }

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

        }).start();
    }
}

我在网上搜了一大堆还是未能解决,我本来当时一个空指针,但是无果,如果各位看出来麻烦纠正

【以下回答由 GPT 生成】

抱歉,但您没有明确提出任何问题。请您详细描述您的问题,我将竭尽全力为您提供解决方案。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^