尝试调用虚方法'void android.view.View.setOnClickListener [duplicate]

满堂花醉三千客,一剑霜寒十四洲。这篇文章主要讲述尝试调用虚方法' void android.view.View.setOnClickListener [duplicate]相关的知识,希望能为你提供帮助。
这个问题在这里已有答案:

  • What is a NullPointerException, and how do I fix it? 12个答案
我正在尝试修改我的应用程序,它适用于片段。我正在尝试添加一个片段,我们称之为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 implementsView.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) {}

有没有办法解决这个问题?应用程序不断崩溃。
答案好像你没有通过默认的findViewById(int id) Activity的方法绑定你的视图引用对象。这就是你获得NPE的原因。
此外,如果您在代码中没有包含视图引用绑定块,则可能尝试使用不存在的id绑定视图或从不同的布局绑定视图。
【尝试调用虚方法' void android.view.View.setOnClickListener [duplicate]】因此,请仔细检查您的布局标记和绑定视图引用代码,它可能会对您有所帮助。

    推荐阅读