InstantiationException:没有零参数构造函数?

Posted

技术标签:

【中文标题】InstantiationException:没有零参数构造函数?【英文标题】:InstantiationException: has no zero argument constructor? 【发布时间】:2016-11-12 16:33:43 【问题描述】:

我正在尝试在谷歌地图中显示位置地址,但是当我为获取地址启动 Intent 服务时,它会显示错误“无法实例化服务”。

我使用此链接显示位置地址。

https://developer.android.com/training/location/display-address.html

这是我的代码:

public class FetchAddressIntentService extends IntentService 
    protected ResultReceiver mReceiver;

    public FetchAddressIntentService(String name) 
        super(name);
    


    @Override
    protected void onHandleIntent(Intent intent) 
        String errorMessage="";

        Geocoder geocoder=new Geocoder(this, Locale.getDefault());

        Location location = intent.getParcelableExtra(Constants.LOCATION_DATA_EXTRA);



        List<Address> addresses=null;

        try 
            addresses=geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),1);
         catch (IOException e) 
            errorMessage="unhendle IO exception";
            e.printStackTrace();
        


        if(addresses==null && addresses.size()==0)
            if(errorMessage.isEmpty())
                errorMessage="no address found";
            

            deliverResultToReceiver(Constants.FAILURE_RESULT, errorMessage);
        
        else

            Address address=addresses.get(0);
            ArrayList<String> arrayListFrag=new ArrayList<>();

            for (int i=0;i<address.getMaxAddressLineIndex();i++)

                arrayListFrag.add(address.getAddressLine(0));
            

            deliverResultToReceiver(Constants.SUCCESS_RESULT,
                    TextUtils.join(System.getProperty("line.separator"),
                            arrayListFrag));

        
    

    private void deliverResultToReceiver(int successResult, String message) 
        Bundle bundle=new Bundle();
        bundle.putString(Constants.RESULT_DATA_KEY,message);
        mReceiver.send(successResult,bundle);
    

这里我开始意图服务:

 protected void startIntentService()
        Intent intent=new Intent(this,FetchAddressIntentService.class);
        intent.putExtra(Constants.RECEIVER, mResultReceiver);
        intent.putExtra(Constants.LOCATION_DATA_EXTRA,mLastLocation);
        startService(intent);
    


   public void fetchAddressButtonHandler()
        if(mGoogleApiClient.isConnected() && mLastLocation!=null)
            startIntentService();
        

   

【问题讨论】:

***.com/a/63651986/3496570 【参考方案1】:

替换:

public FetchAddressIntentService(String name) 
  super(name);

与:

public FetchAddressIntentService() 
  super("whatever you want to use here");

(将字符串替换为适合您应用的字符串)

【讨论】:

onHandleIntent(Intent intent) 服务自动调用??【参考方案2】:

您需要向您的类 FetchAddressIntentService 添加一个零参数构造函数,该构造函数不接受任何参数:

public FetchAddressIntentService() 
    super("FetchAddressIntentService");

【讨论】:

onHandleIntent(Intent intent) 服务自动调用??

以上是关于InstantiationException:没有零参数构造函数?的主要内容,如果未能解决你的问题,请参考以下文章

Xamarin Android java.lang.InstantiationException:java.lang.Class 没有零参数构造函数

线程“主”javax.persistence.PersistenceException 中的异常:org.hibernate.InstantiationException:实体没有默认构造函数:[重复

反射异常 java.lang.InstantiationException处理

Android InstantiationException with Fragment(它是公开的)

MultipartHttpServletRequest BeanInstantiationException 根本原因 java.lang.InstantiationException

firebase:InstantiationException:无法实例化抽象类 java.util.TimeZone