在这种情况下如何解决java.lang.nullpointerexception?

gab6jxml  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(228)

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

什么是nullpointerexception,如何修复它(12个答案)
什么是调试器?它如何帮助我诊断问题(2个答案)
27天前关门了。
给我的答案并不能解决我的问题。
在解释之前,我将向您展示我的(简称)代码:

MainActivity.java:

        public class MainActivity extends AppCompatActivity 
        {
            SecondFragment s = new SecondFragment();

    //Code

            public void conectar(String nombre, String ip, int puerto) {
                Executor executor = Executors.newSingleThreadExecutor();
                executor.execute(new Runnable() {
                        @Override
                        public void run() {
                            Looper.prepare();
                            runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        try{
                                            //Code...
                                            try {
                                                //Code
                                                TabLayout.Tab tab = tabLayout.getTabAt(1);
                                                tab.select();
                                                s.roomText(param1);

                                            catch(SocketTimeoutException ex)
                                            {

                                            }
                                        }
                                        catch(Exception e){
                                            StringWriter sw = new StringWriter();
                                            e.printStackTrace(new PrintWriter(sw));
                                            String stacktrace = sw.toString();
                                            // create an email intent to send to yourself
                                            final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                                            emailIntent.setType("plain/text");
                                            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { "lets.think.android@gmail.com" });
                                            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "App Error Report");
                                            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, stacktrace);
                                            // start the email activity - note you need to start it with a chooser
                                            startActivity(Intent.createChooser(emailIntent, "Send error report..."));
                                        }
                                    }
                                });

                        }
                    });

            }
        }

SecondFragment:

    package com.mycompany.myapp;

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.*;

    public class SecondFragment extends Fragment
    {
        EditText chat, room;

        public SecondFragment() {
    // Required empty public constructor
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_second, container, false);
            room = (EditText) view.findViewById(R.id.mainEditText1);
            chat = (EditText) view.findViewById(R.id.chatEditText1);
            return view;
        }

        public void roomText(String t){
            room.setText(t);
        }
    }

为什么“房间”对象是空的,如果选项卡是事先选择的,而secondfragment的元素实际上应该已经创建了?我不明白。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题