gpt4 book ai didi

java - 错误 - 未注册仪器!必须在注册仪器下运行

转载 作者:行者123 更新时间:2023-12-04 23:13:20 26 4
gpt4 key购买 nike

A) 为什么当我尝试运行应用程序时出现此错误?

我的崩溃日志错误

java.lang.RuntimeException: Unable to start activity java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008) at android.app.ActivityThread.-wrap14(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6688) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) Caused by: java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation. at android.support.test.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:45) at android.support.test.InstrumentationRegistry.getContext(InstrumentationRegistry.java:75) at com.wachipi.challengetv.GuidaTvActivity$GetData.onPreExecute(main.java:76) at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:613) at android.os.AsyncTask.execute(AsyncTask.java:560) at com.wachipi.challengetv.GuidaTvActivity.onCreate(main.java:68) at android.app.Activity.performCreate(Activity.java:6912) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)  at android.app.ActivityThread.-wrap14(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6688)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

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



是什么原因造成的?我还在学习,如果您能解释更多,我将不胜感激。

这是我的代码
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);
}
}
}

最佳答案

您正在尝试在 pDialog = new ProgressDialog(getContext()); 中使用上下文但不是使用 this 这样的上下文( Activity 上下文)或getApplicationContext()您正在使用 InstrumentationRegistry 上下文,在您的情况下它没有用,因为这不是测试,请参阅您的导入 import static android.support.test.InstrumentationRegistry.getContext;只需修复上下文以使用正确的上下文。

关于java - 错误 - 未注册仪器!必须在注册仪器下运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49571367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com