OnclickListener强制关闭。不确定它是否在正确的位置

时间:2014-03-29 10:19:14

标签: android class onclicklistener

好的,所以我有一个模板,可以从assets文件夹安装apk。最近我一直在修改模板以容纳我想添加的功能,所以我基本上已经添加到那里已有的代码。

我添加了2个onclicklisteners,它们都失败并显示错误“找不到处理意图的活动”

继承人我拥有的

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);
    }



    }

第一个onclick来自模板,工作正常。我添加的2然而不是。我刚刚开始在Android和我不确定是否在正确的位置。

2 个答案:

答案 0 :(得分:1)

试试这个..

更改此

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

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

内部点击

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));
        }
    });

答案 1 :(得分:1)

您可以像这样首先初始化按钮

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

然后像这样设置列表器

btn.setOnClickListener(new OnClickListener() {

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

        }
    });

或者在你的代码中只是在OnclickListner部分进行转换,就像这样

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

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

            });

希望它能帮到你,谢谢