满堂花醉三千客,一剑霜寒十四洲。这篇文章主要讲述尝试调用虚方法'
void android.view.View.setOnClickListener [duplicate]相关的知识,希望能为你提供帮助。
这个问题在这里已有答案:
- What is a NullPointerException, and how do I fix it? 12个答案
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]】因此,请仔细检查您的布局标记和绑定视图引用代码,它可能会对您有所帮助。
推荐阅读
- Android studio错误(java.lang.NullPointerException [重复])
- 在android [duplicate]中运行时检查权限时获取NullPointerException
- safari app extensions(从swift后台进程向所有选项卡广播消息)
- Android findViewById空指针[重复]
- 从Safari打开相机APP
- Java applet无法在Safari 7下打开文件(Mac OS X 10.9)
- Safari Webapp强制在单击其他链接时打开Safari
- 限制Google API的Android密钥使用
- 如何在没有app包限制的情况下使用map v2 api key()