Java nosuchmethoderrorandroid
我有一个严重的问题。当我运行测试时,使用浓缩咖啡进行测试后,出现了一个复杂的错误 java.lang.NoSuchMethodError:没有静态方法 setFactory2(Landroid/view/LayoutInflater;Landroid/view/LayoutInflater$Factory2;)V 类内Landroid/support/v4/view/LayoutInflaterCompat;或者它的超级 类(声明'android.support.v4.view.LayoutInflaterCompat' 出现在 /data/app/com.example.master.bakingapp.test-ZVHwTuzx1ipxW0iPyBZKBw==/base.apk) 在 appcompateDelegateImpl.installViewFactory(appcompateDelegateImpl.java:1299) 在 android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:72) 在 com.example.master.bakingapp.MainActivity.onCreate(MainActivity.java:61) 在android.app.Activity.performCreate(Activity.java:7136)的 android.app.Activity.performCreate(Activity.java:7127)位于 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) 在 android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:667) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 位于android.os.Handler.dispatchMessage(Handler.java:106) Looper.loop(Looper.java:193)位于 android.app.ActivityThread.main(ActivityThread.java:6669)位于 java.lang.reflect.Method.invoke(本机方法)位于 RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 测试运行失败:由于“进程”错误,检测运行失败 撞车了。” 它指向我主要活动中的onCreateJava nosuchmethoderrorandroid,java,android,android-studio,Java,Android,Android Studio,我有一个严重的问题。当我运行测试时,使用浓缩咖啡进行测试后,出现了一个复杂的错误 java.lang.NoSuchMethodError:没有静态方法 setFactory2(Landroid/view/LayoutInflater;Landroid/view/LayoutInflater$Factory2;)V 类内Landroid/support/v4/view/LayoutInflaterCompat;或者它的超级 类(声明'android.support.v4.view.LayoutIn
public class RecipeCardFragment extends android.app.Fragment {
public interface OnRecycleClick {
void onclick(int pos);
void jsonPass(String json);
}
private OnRecycleClick mOnRecycleClick;
Recipes[] mRecipes;
boolean mTablet;
//MainActivity
MyViewModel mViewModel;
String json;
public static final String mURL = "https://d17h27t6h515a5.cloudfront.net/topher/2017/May/59121517_baking/baking.json";
ProgressBar progressBar;
////
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mOnRecycleClick=(OnRecycleClick) context;
}catch (ClassCastException e){
//TODO therows new Execption
}
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@SuppressLint("StaticFieldLeak")
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_recipe_recycle,container,false);
final RecyclerView recyclerView=view.findViewById(R.id.recycle_recipe);
//final Context context=container.getContext();
if(savedInstanceState!=null){
mRecipes= (Recipes[]) savedInstanceState.getParcelableArray("Recipes");
}
final SimpleIdlingResource idlingResource = new SimpleIdlingResource();
if (idlingResource != null) {
idlingResource.setIdleState(false);
}
URL url=null;
try {
url=new URL(mURL);
} catch (MalformedURLException e) {
e.printStackTrace();
}
new AsyncTask<URL, Void, String>() {
@Override
protected String doInBackground(URL... urls) {
String jsons="";
try {
jsons= Networkutils.HttpRequest(urls[0]);
} catch (IOException e) {
e.printStackTrace();
}
return jsons;
}
@Override
protected void onPostExecute(String json) {
super.onPostExecute(json);
try {
mRecipes = Networkutils.parseRecipeCard(json);
} catch (JSONException e) {
e.printStackTrace();
}
RecipeCardAdapter recipeCardAdapter= new RecipeCardAdapter(mRecipes,mOnRecycleClick);
mOnRecycleClick.jsonPass(json);
if(mTablet){
GridLayoutManager gridLayoutManager = new
GridLayoutManager(getActivity(), 3);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(recipeCardAdapter);
}
else {
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(recipeCardAdapter);
}
if (idlingResource != null) {
idlingResource.setIdleState(true);
}
}
}.execute(url);
return view;
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArray("Recipes",mRecipes);
}
}
在
android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:72)
在
com.example.master.bakingapp.MainActivity.onCreate(MainActivity.java:61)
我试着调试我的测试。它被困在onCreate中
这是我的全部测试代码
package com.example.master.bakingapp;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.IdlingResource;
import android.support.test.espresso.contrib.RecyclerViewActions;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onData;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.anything;
@RunWith(AndroidJUnit4.class)
public class ClickUiTest {
@Rule
public ActivityTestRule<MainActivity> mActivityTestRule =
new ActivityTestRule<>(MainActivity.class);
private static final String RECIPE_ITEM_BROWNIE = "Brownies";
private IdlingResource mIdlingResource;
// Registers any resource that needs to be synchronized with Espresso before the test is run.
@Before
public void registerIdlingResource() {
mIdlingResource = mActivityTestRule.getActivity().getIdlingResource();
// To prove that the test fails, omit this call:
Espresso.registerIdlingResources(mIdlingResource);
}
@Test
public void checkSelectedItem_InRecycleView() {
onView(ViewMatchers.withId(R.id.recycle_recipe)).perform(RecyclerViewActions.scrollToPosition(1));
onView(withId(R.id.Ingredient_TextView)).check(matches(withText(RECIPE_ITEM_BROWNIE)));
}
@After
public void unregisterIdlingResource() {
if (mIdlingResource != null) {
Espresso.unregisterIdlingResources(mIdlingResource);
}
}
}
主要活动
public class MainActivity extends AppCompatActivity implements RecipeCardFragment.OnRecycleClick
{
private Recipes[] mRecipes;
MyViewModel mViewModel;
String json;
public static final String mURL = "https://d17h27t6h515a5.cloudfront.net/topher/2017/May/59121517_baking/baking.json";
public static final String Bundle_Object = "BUNDLE_OBJ";
ProgressBar progressBar;
FragmentManager fragmentManager;
RecipeCardFragment cardFragment;
boolean mTabletbool;
@Nullable
private SimpleIdlingResource mIdlingResource;
/**
* Only called from test, creates and returns a new {@link SimpleIdlingResource}.
*/
@VisibleForTesting
@NonNull
public IdlingResource getIdlingResource() {
if (mIdlingResource == null) {
mIdlingResource = new SimpleIdlingResource();
}
return mIdlingResource;
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getIdlingResource();
}
@Override
protected void onPostResume() {
super.onPostResume();
}
@Override
public void onclick(int pos) {
Intent intent = new Intent(MainActivity.this, StepsActivity.class);
try {
Recipes recipes;
recipes = Networkutils.ParseJsonIndex(json, pos);
//updateWidget(recipes);
Bundle bundle = new Bundle();
bundle.putParcelable(Bundle_Object, recipes);
intent.putExtras(bundle);
fragmentManager=null;
cardFragment=null;
startActivity(intent);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void jsonPass(String json) {
this.json=json;
}
private void updateWidget(Recipes recipes) {
ArrayList<String> ingredients = new ArrayList<>();
for(Ingredients ingre:recipes.getIngredients()){
String q=ingre.getQuantity()+"";
ingredients.add(ingre.getIngredient()+"\n"+
"Quantity: "+q+"\n"+
"Measure: "+ingre.getMeasure()+"\n");
}
UpdateBakingService.startBakingService(MainActivity.this,ingredients);
}
public类MainActivity扩展AppCompatActivity实现RecipeCardFragment.OnRecycleClick
{
私人食谱[]种;
MyViewModel mViewModel;
字符串json;
公共静态最终字符串mURL=”https://d17h27t6h515a5.cloudfront.net/topher/2017/May/59121517_baking/baking.json";
公共静态最终字符串Bundle\u Object=“Bundle\u OBJ”;
ProgressBar ProgressBar;
碎片管理器碎片管理器;
RecipeCardFragment-cardFragment;
布尔mTabletbool;
@可空
私有的简单资源;
/**
*仅从测试调用,创建并返回一个新的{@link SimpleIdlingResource}。
*/
@可视性测试
@非空
公共IdlingResource getIdlingResource(){
if(mIdlingResource==null){
mIdlingResource=新的SimpleIdlingResource();
}
回归资源;
}
@凌驾
创建时受保护的void(最终捆绑包savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getIdlingResource();
}
@凌驾
PostResume()上的受保护无效{
super.onPostResume();
}
@凌驾
公共void onclick(内部位置){
意向意向=新意向(MainActivity.this、StepsActivity.class);
试一试{
食谱;
recipes=Networkutils.ParseJsonIndex(json,pos);
//更新网页(食谱);
Bundle=新Bundle();
bundle.putParcelable(bundle\u对象、配方);
意向。额外支出(捆绑);
fragmentManager=null;
cardFragment=null;
星触觉(意向);
}捕获(JSONException e){
e、 printStackTrace();
}
}
@凌驾
公共void jsonPass(字符串json){
this.json=json;
}
私有void updateWidget(配方配方){
ArrayList Components=新的ArrayList();
对于(配料:recipes.getComponents()){
字符串q=ingre.getQuantity()+“”;
配料。添加(ingre.GetComponent()+“\n”+
数量:“+q+”\n+
度量值:“+ingre.getMeasure()+”\n”);
}
UpdateBakingService.startBakingService(MainActivity.this,Components);
}
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.master.bakingapp.Fragments.RecipeCardFragment"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"/>
我的片段由MainActivity托管
public class RecipeCardFragment extends android.app.Fragment {
public interface OnRecycleClick {
void onclick(int pos);
void jsonPass(String json);
}
private OnRecycleClick mOnRecycleClick;
Recipes[] mRecipes;
boolean mTablet;
//MainActivity
MyViewModel mViewModel;
String json;
public static final String mURL = "https://d17h27t6h515a5.cloudfront.net/topher/2017/May/59121517_baking/baking.json";
ProgressBar progressBar;
////
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mOnRecycleClick=(OnRecycleClick) context;
}catch (ClassCastException e){
//TODO therows new Execption
}
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@SuppressLint("StaticFieldLeak")
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_recipe_recycle,container,false);
final RecyclerView recyclerView=view.findViewById(R.id.recycle_recipe);
//final Context context=container.getContext();
if(savedInstanceState!=null){
mRecipes= (Recipes[]) savedInstanceState.getParcelableArray("Recipes");
}
final SimpleIdlingResource idlingResource = new SimpleIdlingResource();
if (idlingResource != null) {
idlingResource.setIdleState(false);
}
URL url=null;
try {
url=new URL(mURL);
} catch (MalformedURLException e) {
e.printStackTrace();
}
new AsyncTask<URL, Void, String>() {
@Override
protected String doInBackground(URL... urls) {
String jsons="";
try {
jsons= Networkutils.HttpRequest(urls[0]);
} catch (IOException e) {
e.printStackTrace();
}
return jsons;
}
@Override
protected void onPostExecute(String json) {
super.onPostExecute(json);
try {
mRecipes = Networkutils.parseRecipeCard(json);
} catch (JSONException e) {
e.printStackTrace();
}
RecipeCardAdapter recipeCardAdapter= new RecipeCardAdapter(mRecipes,mOnRecycleClick);
mOnRecycleClick.jsonPass(json);
if(mTablet){
GridLayoutManager gridLayoutManager = new
GridLayoutManager(getActivity(), 3);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(recipeCardAdapter);
}
else {
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(recipeCardAdapter);
}
if (idlingResource != null) {
idlingResource.setIdleState(true);
}
}
}.execute(url);
return view;
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArray("Recipes",mRecipes);
}
}
公共类RecipeCardFragment扩展了android.app.Fragment{
公共接口OnRecycleClick{
void onclick(内部位置);
void jsonPass(字符串json);
}
私人OnRecycleClick mOnRecycleClick;
食谱[]种;
布尔函数;
//主要活动
MyViewModel mViewModel;
字符串json;
公共静态最终字符串mURL=”https://d17h27t6h515a5.cloudfront.net/topher/2017/May/59121517_baking/baking.json";
ProgressBar ProgressBar;
////
@凌驾
公共void-onAttach(上下文){
super.onAttach(上下文);
试一试{
mOnRecycleClick=(OnRecycleClick)上下文;
}catch(ClassCastException e){
//TODO therows新执行选项
}
}
@凌驾
创建时的公共void(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@SuppressLint(“StaticFieldLeak”)
@可空
@凌驾
创建视图时的公共视图(@NonNull LayoutInflater inflater、@Nullable ViewGroup container、@Nullable Bundle savedInstanceState){
视图=充气机。充气(R.布局。碎片\配方\回收,容器,错误);
static void setFactory(LayoutInflater inflater, LayoutInflaterFactory factory) {
Factory2 factory2 = factory != null ? new FactoryWrapperHC(factory) : null;
inflater.setFactory2(factory2);
Factory f = inflater.getFactory();
if (f instanceof Factory2) {
forceSetFactory2(inflater, (Factory2) f);
} else {
forceSetFactory2(inflater, factory2);
}
}