尝试调用虚拟方法' void android.view.View.setOnClickListener

时间:2017-12-30 23:32:11

标签: android nullpointerexception

我正在尝试修改适用于片段的应用。我试图添加一个片段,让我们称之为tab_1,来自其他应用程序的一个视图。这是一个天气应用程序,用户点击按钮后会下载数据,因此我使用了导致问题的列表器

E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.example.chirag.slidingtabsusingviewpager, PID: 4586
                                                 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                     at com.example.chirag.slidingtabsusingviewpager.Tab1.onCreate(Tab1.java:100)
                                                     at android.support.v4.app.Fragment.performCreate(Fragment.java:2226)
                                                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)

这里是tab_1.class的代码

public class Tab1 extends Fragment implements  View.OnClickListener  {


    private OnFragmentInteractionListener mListener;

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

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

    public static Tab1 newInstance(String param1, String param2) {
        Tab1 fragment = new Tab1();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        getActivity().setContentView(R.layout.activity_main);
        sharedPreferences = getActivity().getSharedPreferences(PREFERENCES_NAME, MainActivity.MODE_PRIVATE);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!isOnline()) {
                    textViewTemperature.setText(restoreData("TEMP"));
                    Toast.makeText(getActivity().getApplicationContext(), "Cannot connect to network, data will be restore from file with last downloaded data...", Toast.LENGTH_LONG).show();
                } else {
                    Thread t = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                weather = parser.getWeatherForLocation(editText.getText() + "");
                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        editText.setText(weather.getCity() + ",  " + weather.getCountry());
                                        textViewTemperature.setText("Temperature: " + weather.getTemperature());
                                        saveData(textViewTemperature.getText() + "", "TEMP");
                                    }
                                });
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    t.start();
                }
            }
        });
        sharedPreferences = getActivity().getSharedPreferences("file_name", getActivity().MODE_PRIVATE);
        buttonExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
                System.exit(0);
            }
        });
        buttonAbout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity().getApplicationContext(), AboutActivity.class));
            }
        });
    }
  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_tab1, container, false);
    }

    @Override
    public void onClick(View v) {

    }

有没有办法解决这个问题? 应用程序不断崩溃。

1 个答案:

答案 0 :(得分:0)

似乎您没有通过默认的findViewById(int id) Activity方法绑定视图引用对象。这就是你获得NPE的原因。

此外,如果您在代码中未包含视图引用绑定块,则可能尝试使用不存在的id绑定视图或从不同的布局绑定视图。

因此,请仔细检查您的布局标记和绑定视图引用代码,它可能对您有帮助。