我正在使用Hibernate将项目连接到MySQL,并且hibernate.cfg.xml文件出现错误-无法解析mysql包(以及cj,jdbc等包)。
这是代码-
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_menu, container, false);
final MenuActivity menuAct = (MenuActivity) getActivity();
final MenuViewModel viewModel = ViewModelProviders.of(this, new MenuViewModelFactory(menuAct.getApplication(), MenuActivity.ResID)).get(MenuViewModel.class);
viewModel.getAllMenuTitles().observe(this, new Observer<String[]>() {
@Override
public void onChanged(String[] strings) {
final Spinner spinner = rootView.findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), R.layout.support_simple_spinner_dropdown_item, strings);
adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
menuAct.removeAllTabs();
SubMenuViewModel vm = ViewModelProviders.of(getParentFragment(), new SubMenuViewModelFactory(menuAct.getApplication(), i)).get(SubMenuViewModel.class); //← This is the problematic getApplication()
vm.getSubMenus().observe(getActivity(), new Observer<String[]>() {
@Override
public void onChanged(@Nullable String[] strings) {
menuAct.populateTabs(strings);
}
});
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
});
return rootView;
}
代码中的第五行是我在说的。
问题是正确地声明了依赖项,并且在我的项目合作伙伴处可以使用相同的文件。
(Spring框架)。
#update 1- 增加了依赖性
java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentActivity android.support.v4.app.Fragment.getActivity()' on a null object reference
at android.arch.lifecycle.ViewModelProviders.checkActivity(ViewModelProviders.java:50)
at android.arch.lifecycle.ViewModelProviders.of(ViewModelProviders.java:100)
at com.whatamidoingwithmylife.splitbill.MenuActivity$PlaceholderFragment$1$1.onItemSelected(MenuActivity.java:198)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:944)
at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:933)
at android.widget.AdapterView.-wrap1(Unknown Source:0)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:898)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
堆栈
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/theprocess?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC</property>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">Amit4089</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- DB schema will be updated if needed -->
<property name="hbm2ddl.auto">update</property>
<mapping class="com.example.WebAppProcess20.Entities.ClientsEntity"/>
<mapping class="com.example.WebAppProcess20.Entities.InvoicesEntity"/>
<mapping class="com.example.WebAppProcess20.Entities.OrdersEntity"/>
<mapping class="com.example.WebAppProcess20.Entities.OrdersitemsEntity"/>
<mapping class="com.example.WebAppProcess20.Entities.ProductsEntity"/>
</session-factory>
</hibernate-configuration>
` 有什么想法吗?
答案 0 :(得分:0)
从您的帖子中看不到错误。但是请确保MySQLDialect
的版本与您使用的MySQL版本兼容。
还有其他MySQLDialect
适用于5.x之前的版本。然后分别有5.5版和5.7版的MySQL55Dialect
和MySQL57Dialect
。