Note that there are some explanatory texts on larger screens.

plurals
  1. POInstantiationException occur while Downloading using Service
    primarykey
    data
    text
    <p>I was trying to do a service sample program and i am getting following exception</p> <blockquote> <p>09-10 20:57:57.871: E/AndroidRuntime(280): FATAL EXCEPTION: main 09-10 20:57:57.871: E/AndroidRuntime(280): java.lang.RuntimeException: Unable to instantiate service com.example.demoservice.DownloadService: java.lang.InstantiationException: com.example.demoservice.DownloadService</p> </blockquote> <p>I have seen many solutions to this type of execption like passing string to constructor etc.But those solutions didnt solved this issue.</p> <p>Code sample is given below</p> <pre><code>public class MainActivity extends Activity { TextView textView ; private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Bundle bundle = intent.getExtras(); if(bundle != null){ String filepath = bundle.getString(DownloadService.FILEPATH); int result = bundle.getInt(DownloadService.RESULT); if(result == Activity.RESULT_OK){ Toast.makeText(context, "Sucess" + filepath, Toast.LENGTH_SHORT).show(); } else{ Toast.makeText(context, "Sucess", Toast.LENGTH_SHORT).show(); } } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.status); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void onClick(View v){ Intent i = new Intent(this, DownloadService.class); i.putExtra(DownloadService.FILENAME, "index.html"); i.putExtra(DownloadService.URL, "http://www.vogella.com/index.html"); startService(i); textView.setText("Service started"); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); registerReceiver(receiver, new IntentFilter(DownloadService.NOTIFICATION)); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); unregisterReceiver(receiver); } </code></pre> <p>}</p> <p>Service class</p> <pre><code>public class DownloadService extends IntentService{ private int result = Activity.RESULT_CANCELED; public static final String URL = "urlpath"; public static final String FILENAME = "filename"; public static final String FILEPATH = "filepath"; public static final String RESULT = "result"; public static final String NOTIFICATION = "com.vogella.android.service.receiver"; public DownloadService(String name) { super("DownloadService"); // TODO Auto-generated constructor stub } @Override protected void onHandleIntent(Intent intent) { // TODO Auto-generated method stub String urlpath = intent.getStringExtra(URL); String filename = intent.getStringExtra(urlpath); File output = new File(Environment.getExternalStorageDirectory(), filename); if(output.exists()){ output.delete(); } InputStream input = null; FileOutputStream fout = null; try { java.net.URL url = new java.net.URL(urlpath); input = url.openConnection().getInputStream(); InputStreamReader reader = new InputStreamReader(input); fout = new FileOutputStream(output.getPath()); int next = -1; while((next = reader.read())!= -1){ fout.write(next); } result = Activity.RESULT_OK; } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ if(input != null){ try { input.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(fout != null){ try { fout.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } publishResult( output.getAbsoluteFile(), result); } private void publishResult(File absoluteFile, int result2) { // TODO Auto-generated method stub Intent intent = new Intent(this,DownloadService.class); intent.putExtra(FILEPATH, absoluteFile); intent.putExtra(RESULT, result2); sendBroadcast(intent); } </code></pre> <p>}</p> <p>I am running app using Emulator.Is it possible to run this problem in emulator,because writing to external directory</p> <p>Can anyone help me?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload