Error - No instrumentation registered! Must run under a registering instrumentation

匿名 (未验证) 提交于 2019-12-03 01:34:02

问题:

A) Why when i try to run the app i get this error?

My Crash Log Error

Unable to start activity ComponentInfojava.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.

What caused that? I'm yet learning and i'll appreciate if you can explain more.

This is my code

import android.app.ProgressDialog; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import android.support.design.widget.TabLayout; import android.widget.ListView; import android.widget.Toast; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.HashMap; import java.util.List;     import static android.support.test.InstrumentationRegistry.getContext; import static com.google.android.gms.internal.zzahn.runOnUiThread;    /**  * Created by GB on 28/03/18.  */  public class Main extends AppCompatActivity {      private Toolbar toolbar;     private TabLayout tabLayout;     private ViewPager viewPager;     private ProgressDialog pDialog;     //url per il recupero del JSON     private static String url = "there is an url trust me. But i need to hide";      private ListView TodayMenu;      ArrayList<HashMap<String, String>> DATA;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);          Toolbar toolbargtv = (Toolbar) findViewById(R.id.toolbargtv);         setSupportActionBar(toolbargtv);          //getSupportActionBar().setDisplayHomeAsUpEnabled(true);          ViewPager viewer = (ViewPager) findViewById(R.id.viewer);         setupViewPager(viewer);          TabLayout tabsgtv = (TabLayout) findViewById(R.id.tabsgtv);         tabsgtv.setupWithViewPager(viewer);          DATA = new ArrayList<>();          TodayMenu = (ListView) findViewById(R.id.todayMenu);          new GetData().execute();     }      private class GetData extends AsyncTask<Void,Void,Void>{         @Override         protected void onPreExecute(){             super.onPreExecute();             //Mostro avviso             pDialog = new ProgressDialog(getContext());             pDialog.setMessage("Attendi..");             pDialog.setCancelable(false);             pDialog.show();         }          @Override         protected Void doInBackground(Void... arg0){             HttpHandler sh = new HttpHandler();             //faccio una richiesta al server e attendo risposta             String jsonStr = sh.makeServiceCall(url);              Log.e("RAW-JSON: ","Retrieve RAW-Json is "+jsonStr);              if (jsonStr != null) {                 try {                     JSONObject jsonObj = new JSONObject(jsonStr);                     String DATESTRING = jsonObj.getString("date");                      JSONObject DATE = new JSONObject(DATESTRING); //Log.d("STAMPA","DATESTRING"+DATESTRING);                      //Log.d("PER FAVORE","IERI ERA "+Ieri);                     //ESTRAGGO E INSERISCO DOVE MI SERVE                     String Yesterday = DATE.getString("ieri");                     String Today = DATE.getString("oggi");                     String Tomorrow = DATE.getString("domani");                 } catch (JSONException e) {                     runOnUiThread(new Runnable(){                         @Override                         public void run(){                             Toast.makeText(getContext(),"Errore nel recupero dati",Toast.LENGTH_LONG).show();                         }                     });                     e.printStackTrace();                 }             }              return null;         }         @Override         protected void onPostExecute(Void result){             super.onPostExecute(result);             //tolgo il caricamento di pDialog             if (pDialog.isShowing())                 pDialog.dismiss();         }     }      private void setupViewPager(ViewPager viewPager) {         ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());         adapter.addFragment(new YesterdayFragment(),"" );  //i can set from here the name of tabs but how is the name from by Json?         adapter.addFragment(new TodayFragment(), "");         adapter.addFragment(new TomorrowFragment(), "");         viewPager.setAdapter(adapter);     }      class ViewPagerAdapter extends FragmentPagerAdapter {         private final List<Fragment> mFragmentList = new ArrayList<>();         private final List<String> mFragmentTitleList = new ArrayList<>();          public ViewPagerAdapter(FragmentManager manager) {             super(manager);         }          @Override         public Fragment getItem(int position) {             return mFragmentList.get(position);         }          @Override         public int getCount() {             return mFragmentList.size();         }          public void addFragment(Fragment fragment, String title) {             mFragmentList.add(fragment);             mFragmentTitleList.add(title);         }          @Override         public CharSequence getPageTitle(int position) {             return mFragmentTitleList.get(position);         }     } } 

回答1:

You are trying to use a context in pDialog = new ProgressDialog(getContext()); but instead of using the context like this(activity context) or getApplicationContext() you are using an InstrumentationRegistry context, which in your case is not useful because that's not a test, see your import import static android.support.test.InstrumentationRegistry.getContext; Just fix the context to use the right one.



回答2:

Remove this line import static android.support.test.InstrumentationRegistry.getContext;

then you need to pass the context (using this or anything similar) of the activity instead.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!