Roku
Roku

Reputation: 1

OnclickListener force closes. not sure if its in the right place

ok so i have a template that installs an apk from the assets folder. recently ive been modding the template to accomodate features that i wanted to add, so im basically adding to code that was there already.

ive added 2 onclicklisteners and both of them fail with the error "No activity found to handle the intent"

heres what i have

package com.example.depthtiles;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.example.depthtiles.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.Toast;

public class MainActivity extends Activity {

    // change this to your apk skin name
    private static final String ZOOPER_APK = "Depth Zooper.apk";




    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        context = this;
        setContentView(R.layout.activity_main);
        ScrollView sView = (ScrollView)findViewById(R.id.ScrollView02);
        //Hide the Scrollbar
        sView.setVerticalScrollBarEnabled(false);
        sView.setHorizontalScrollBarEnabled(false);

        findViewById(R.id.InstallSkinButton).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        showInstallableSkins();
                    }

                });
        findViewById(R.id.InstallIconButton).setOnClickListener(
                new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent("com.example.depthfinal.ICONBUTTON"));
            }
        });

        findViewById(R.id.ButtonGmail).setOnClickListener(
                new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent("com.example.depthfinal.CONTACTGMAIL"));
            }
        });



    }

    private class RepairSkinAsyncTask extends AsyncTask<Void, Void, Void> {

        private ProgressDialog mDialog;

        @Override
        protected void onPreExecute() {
            mDialog = ProgressDialog.show(context, "", "Processing...", true);
        }

        @Override
        protected Void doInBackground(Void... nothing) {
            String SDCARD_MYAPK_APK = Environment.getExternalStorageDirectory()
                    .getPath() + File.separator + "my_temporary_skin_apk.apk";
            deleteOldSkin(SDCARD_MYAPK_APK);
            saveSkinToSdCard(SDCARD_MYAPK_APK);
            startAppInstaller(SDCARD_MYAPK_APK);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            mDialog.dismiss();
            finish();
        }
    }

    /**
     * 
     */
    private void showInstallableSkins() {
        if (isSDcardAvailable()) {
            new RepairSkinAsyncTask().execute();
        } else {
            Toast.makeText(this, "SD card not available", Toast.LENGTH_LONG)
                    .show();
        }

    }

    private void deleteOldSkin(String pathToSkin) {
        File file = new File(pathToSkin);
        if (file.exists()) {
            file.delete();
        }
    }

    /**
     * @param assetManager
     * @param in
     * @param out
     * @param pathToSkin
     */
    private void saveSkinToSdCard(String pathToSkin) {
        AssetManager assetManager = getAssets();

        InputStream in = null;
        OutputStream out = null;

        try {
            in = assetManager.open(ZOOPER_APK);
            try {
                out = new FileOutputStream(pathToSkin);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            byte[] buffer = new byte[1024];
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }

            in.close();
            in = null;

            out.flush();

            out.close();

            out = null;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param pathToSkin
     */
    private void startAppInstaller(String pathToSkin) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(pathToSkin)),
                "application/vnd.android.package-archive");
        startActivity(intent);
    }

    private boolean isSDcardAvailable() {
        String state = Environment.getExternalStorageState();
        return state.contentEquals(Environment.MEDIA_MOUNTED)
                || state.contentEquals(Environment.MEDIA_MOUNTED_READ_ONLY);
    }



    }

the first onclick was there from the template and works fine. the 2 that i added however do not. Im just starting out in android and im not sure if there in the right spot.

Upvotes: 0

Views: 131

Answers (2)

Irshad Khan
Irshad Khan

Reputation: 784

You can either initialise Button first like this

Button btn=(Button)findViewById(R.id.btn_st_open2);

then set listner on it like this

btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // do something you want

        }
    });

or in your code just do casting at The OnclickListner part like this

 (Button)findViewById(R.id.InstallSkinButton).setOnClickListener(
            new OnClickListener() {

                @Override
                public void onClick(View v) {
                    showInstallableSkins();
                }

            });

hopefully it will help you thanks

Upvotes: 1

Hariharan
Hariharan

Reputation: 24853

Try this..

Change this

startActivity(new Intent("com.example.depthfinal.ICONBUTTON"));
startActivity(new Intent("com.example.depthfinal.CONTACTGMAIL"));

to

startActivity(new Intent(MainActivity.this,ICONBUTTON.class));
startActivity(new Intent(MainActivity.this,CONTACTGMAIL.class));

Inside Click

findViewById(R.id.InstallIconButton).setOnClickListener(
            new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            startActivity(new Intent(MainActivity.this,ICONBUTTON.class));
        }
    });

Upvotes: 1

Related Questions