"不幸的是,...已经停止"在Android的下载文件时, [英] "Unfortunately, ... has stopped" when Downloading file in Android

查看:232
本文介绍了"不幸的是,...已经停止"在Android的下载文件时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Android的Studio中的应用程序,它要求你将文件下载到用户设备上的SD卡/下载/文件夹。问题是,当我输入从下载并点击下载网址,应用程序说不幸的是,*已停止。我有好几个文件,以便他们在这里。

I'm trying to make an app in Android Studio which requires you to download a file to the sdcard/Download/ folder on the users device. The problem is that when I enter the URL to download from and click Download, the app says "Unfortunately, * has stopped". I have quite a few files so here they are.

首先,继承人的清单...

First, heres the Manifest...

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.NautGames.xecta.app" >

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.NautGames.xecta.app.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<!-- Permission: Writing to SDCard -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

</manifest>

继承人的MainActivity类...

Heres the MainActivity class...

package com.NautGames.xecta.app;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
//Chat bot library
import org.alicebot.ab.Chat;
import org.alicebot.ab.Bot;

import android.view.View;
import android.widget.Button;
import android.os.Environment;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

TextView input;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

//EditText mEdit = (EditText)findViewById(R.id.editText1);

public void buttonOnClick(View v)
{
    input = (TextView) findViewById(R.id.editText1);

    String dbPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/Ab";

    Button button=(Button) v;

    //Creating bot
    String botname="xecta";
    String path= dbPath;
    Bot xecta = new Bot(botname, path);

    Chat chatSession = new Chat(xecta);

    String request = input.getText().toString();
    String response = chatSession.multisentenceRespond(request);
    ((Button) v).setText(response);
}

public void buttonDownload(View v)
{

}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

DownloaderThread(下载文件)...

DownloaderThread (to download file)...

package com.NautGames.xecta.app;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.os.Environment;
import android.os.Message;

/**
 * Downloads a file in a thread. Will send messages to the
 * AndroidFileDownloader activity to update the progress bar.
 */
public class DownloaderThread extends Thread
{
// constants
private static final int DOWNLOAD_BUFFER_SIZE = 4096;

// instance variables
private AndroidFileDownloader parentActivity;
private String downloadUrl;

/**
 //Instantiates a new DownloaderThread object.
 // @param parentActivity Reference to AndroidFileDownloader activity.
 // @param inUrl String representing the URL of the file to be downloaded.
 */
public DownloaderThread(AndroidFileDownloader inParentActivity, String inUrl)
{
    downloadUrl = "";
    if(inUrl != null)
    {
        downloadUrl = inUrl;
    }
    parentActivity = inParentActivity;
}

/**
 * Connects to the URL of the file, begins the download, and notifies the
 * AndroidFileDownloader activity of changes in state. Writes the file to
 * the root of the SD card.
 */
@Override
public void run()
{
    URL url;
    URLConnection conn;
    int fileSize, lastSlash;
    String fileName;
    BufferedInputStream inStream;
    BufferedOutputStream outStream;
    File outFile;
    FileOutputStream fileStream;
    Message msg;

    // we're going to connect now
    msg = Message.obtain(parentActivity.activityHandler,
            AndroidFileDownloader.MESSAGE_CONNECTING_STARTED,
            0, 0, downloadUrl);
    parentActivity.activityHandler.sendMessage(msg);

    try
    {
        url = new URL(downloadUrl);
        conn = url.openConnection();
        conn.setUseCaches(false);
        fileSize = conn.getContentLength();

        // get the filename
        lastSlash = url.toString().lastIndexOf('/');
        fileName = "file.bin";
        if(lastSlash >=0)
        {
            fileName = url.toString().substring(lastSlash + 1);
        }
        if(fileName.equals(""))
        {
            fileName = "file.bin";
        }

        // notify download start
        int fileSizeInKB = fileSize / 1024;
        msg = Message.obtain(parentActivity.activityHandler,
                AndroidFileDownloader.MESSAGE_DOWNLOAD_STARTED,
                fileSizeInKB, 0, fileName);
        parentActivity.activityHandler.sendMessage(msg);

        // start download
        inStream = new BufferedInputStream(conn.getInputStream());
        outFile = new File(Environment.getExternalStorageDirectory() + "/" + fileName);
        fileStream = new FileOutputStream(outFile);
        outStream = new BufferedOutputStream(fileStream, DOWNLOAD_BUFFER_SIZE);
        byte[] data = new byte[DOWNLOAD_BUFFER_SIZE];
        int bytesRead = 0, totalRead = 0;
        while(!isInterrupted() && (bytesRead = inStream.read(data, 0, data.length)) >= 0)
        {
            outStream.write(data, 0, bytesRead);

            // update progress bar
            totalRead += bytesRead;
            int totalReadInKB = totalRead / 1024;
            msg = Message.obtain(parentActivity.activityHandler,
                    AndroidFileDownloader.MESSAGE_UPDATE_PROGRESS_BAR,
                    totalReadInKB, 0);
            parentActivity.activityHandler.sendMessage(msg);
        }

        outStream.close();
        fileStream.close();
        inStream.close();

        if(isInterrupted())
        {
            // the download was canceled, so let's delete the partially downloaded file
            outFile.delete();
        }
        else
        {
            // notify completion
            msg = Message.obtain(parentActivity.activityHandler,
                    AndroidFileDownloader.MESSAGE_DOWNLOAD_COMPLETE);
            parentActivity.activityHandler.sendMessage(msg);
        }
    }
    catch(MalformedURLException e)
    {
        String errMsg = parentActivity.getString(R.string.error_message_bad_url);
        msg = Message.obtain(parentActivity.activityHandler,
                AndroidFileDownloader.MESSAGE_ENCOUNTERED_ERROR,
                0, 0, errMsg);
        parentActivity.activityHandler.sendMessage(msg);
    }
    catch(FileNotFoundException e)
    {
        String errMsg = parentActivity.getString(R.string.error_message_file_not_found);
        msg = Message.obtain(parentActivity.activityHandler,
                AndroidFileDownloader.MESSAGE_ENCOUNTERED_ERROR,
                0, 0, errMsg);
        parentActivity.activityHandler.sendMessage(msg);
    }
    catch(Exception e)
    {
        String errMsg = parentActivity.getString(R.string.error_message_general);
        msg = Message.obtain(parentActivity.activityHandler,
                AndroidFileDownloader.MESSAGE_ENCOUNTERED_ERROR,
                0, 0, errMsg);
        parentActivity.activityHandler.sendMessage(msg);
    }
}

}

最后,该AndroidFileDownload(也下载文件)...

And lastly, The AndroidFileDownload(also to download file)...

package com.NautGames.xecta.app;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AndroidFileDownloader extends Activity implements OnClickListener
{
// Used to communicate state changes in the DownloaderThread
public static final int MESSAGE_DOWNLOAD_STARTED = 1000;
public static final int MESSAGE_DOWNLOAD_COMPLETE = 1001;
public static final int MESSAGE_UPDATE_PROGRESS_BAR = 1002;
public static final int MESSAGE_DOWNLOAD_CANCELED = 1003;
public static final int MESSAGE_CONNECTING_STARTED = 1004;
public static final int MESSAGE_ENCOUNTERED_ERROR = 1005;

// instance variables
private AndroidFileDownloader thisActivity;
private Thread downloaderThread;
private ProgressDialog progressDialog;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    thisActivity = this;
    downloaderThread = null;
    progressDialog = null;
    setContentView(R.layout.activity_main);
    Button button = (Button) this.findViewById(R.id.download_button);
    button.setOnClickListener(this);
}

/** Called when the user clicks on something. */
@Override
public void onClick(View view)
{
    EditText urlInputField = (EditText) this.findViewById(R.id.url_input);
    String urlInput = urlInputField.getText().toString();
    downloaderThread = new DownloaderThread(thisActivity, urlInput);
    downloaderThread.start();
}

/**
 * This is the Handler for this activity. It will receive messages from the
 * DownloaderThread and make the necessary updates to the UI.
 */
public Handler activityHandler = new Handler()
{
    public void handleMessage(Message msg)
    {
        switch(msg.what)
        {
                            /*
                             * Handling MESSAGE_UPDATE_PROGRESS_BAR:
                             * 1. Get the current progress, as indicated in the arg1 field
                             *    of the Message.
                             * 2. Update the progress bar.
                             */
            case MESSAGE_UPDATE_PROGRESS_BAR:
                if(progressDialog != null)
                {
                    int currentProgress = msg.arg1;
                    progressDialog.setProgress(currentProgress);
                }
                break;

                            /*
                             * Handling MESSAGE_CONNECTING_STARTED:
                             * 1. Get the URL of the file being downloaded. This is stored
                             *    in the obj field of the Message.
                             * 2. Create an indeterminate progress bar.
                             * 3. Set the message that should be sent if user cancels.
                             * 4. Show the progress bar.
                             */
            case MESSAGE_CONNECTING_STARTED:
                if(msg.obj != null && msg.obj instanceof String)
                {
                    String url = (String) msg.obj;
                    // truncate the url
                    if(url.length() > 16)
                    {
                        String tUrl = url.substring(0, 15);
                        tUrl += "...";
                        url = tUrl;
                    }
                    String pdTitle = thisActivity.getString(R.string.progress_dialog_title_connecting);
                    String pdMsg = thisActivity.getString(R.string.progress_dialog_message_prefix_connecting);
                    pdMsg += " " + url;

                    dismissCurrentProgressDialog();
                    progressDialog = new ProgressDialog(thisActivity);
                    progressDialog.setTitle(pdTitle);
                    progressDialog.setMessage(pdMsg);
                    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                    progressDialog.setIndeterminate(true);
                    // set the message to be sent when this dialog is canceled
                    Message newMsg = Message.obtain(this, MESSAGE_DOWNLOAD_CANCELED);
                    progressDialog.setCancelMessage(newMsg);
                    progressDialog.show();
                }
                break;

                            /*
                             * Handling MESSAGE_DOWNLOAD_STARTED:
                             * 1. Create a progress bar with specified max value and current
                             *    value 0; assign it to progressDialog. The arg1 field will
                             *    contain the max value.
                             * 2. Set the title and text for the progress bar. The obj
                             *    field of the Message will contain a String that
                             *    represents the name of the file being downloaded.
                             * 3. Set the message that should be sent if dialog is canceled.
                             * 4. Make the progress bar visible.
                             */
            case MESSAGE_DOWNLOAD_STARTED:
                // obj will contain a String representing the file name
                if(msg.obj != null && msg.obj instanceof String)
                {
                    int maxValue = msg.arg1;
                    String fileName = (String) msg.obj;
                    String pdTitle = thisActivity.getString(R.string.progress_dialog_title_downloading);
                    String pdMsg = thisActivity.getString(R.string.progress_dialog_message_prefix_downloading);
                    pdMsg += " " + fileName;

                    dismissCurrentProgressDialog();
                    progressDialog = new ProgressDialog(thisActivity);
                    progressDialog.setTitle(pdTitle);
                    progressDialog.setMessage(pdMsg);
                    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    progressDialog.setProgress(0);
                    progressDialog.setMax(maxValue);
                    // set the message to be sent when this dialog is canceled
                    Message newMsg = Message.obtain(this, MESSAGE_DOWNLOAD_CANCELED);
                    progressDialog.setCancelMessage(newMsg);
                    progressDialog.setCancelable(true);
                    progressDialog.show();
                }
                break;

                            /*
                             * Handling MESSAGE_DOWNLOAD_COMPLETE:
                             * 1. Remove the progress bar from the screen.
                             * 2. Display Toast that says download is complete.
                             */
            case MESSAGE_DOWNLOAD_COMPLETE:
                dismissCurrentProgressDialog();
                displayMessage(getString(R.string.user_message_download_complete));
                break;

                            /*
                             * Handling MESSAGE_DOWNLOAD_CANCELLED:
                             * 1. Interrupt the downloader thread.
                             * 2. Remove the progress bar from the screen.
                             * 3. Display Toast that says download is complete.
                             */
            case MESSAGE_DOWNLOAD_CANCELED:
                if(downloaderThread != null)
                {
                    downloaderThread.interrupt();
                }
                dismissCurrentProgressDialog();
                displayMessage(getString(R.string.user_message_download_canceled));
                break;

                            /*
                             * Handling MESSAGE_ENCOUNTERED_ERROR:
                             * 1. Check the obj field of the message for the actual error
                             *    message that will be displayed to the user.
                             * 2. Remove any progress bars from the screen.
                             * 3. Display a Toast with the error message.
                             */
            case MESSAGE_ENCOUNTERED_ERROR:
                // obj will contain a string representing the error message
                if(msg.obj != null && msg.obj instanceof String)
                {
                    String errorMessage = (String) msg.obj;
                    dismissCurrentProgressDialog();
                    displayMessage(errorMessage);
                }
                break;

            default:
                // nothing to do here
                break;
        }
    }
};

/**
 * If there is a progress dialog, dismiss it and set progressDialog to
 * null.
 */
public void dismissCurrentProgressDialog()
{
    if(progressDialog != null)
    {
        progressDialog.hide();
        progressDialog.dismiss();
        progressDialog = null;
    }
}

/**
 * Displays a message to the user, in the form of a Toast.
 * @param message Message to be displayed.
 */
public void displayMessage(String message)
{
    if(message != null)
    {
        Toast.makeText(thisActivity, message, Toast.LENGTH_SHORT).show();
    }
}
}

和继承人的日志:

Process: com.NautGames.xecta.app, PID: 3032
    java.lang.IllegalStateException: Could not find a method onClick(View) in the                       activity class com.NautGames.xecta.app.MainActivity for onClick handler on view class              android.widget.Button with id 'download_button'
    at android.view.View$1.onClick(View.java:3810)
    at android.view.View.performClick(View.java:4438)
    at android.view.View$PerformClick.run(View.java:18422)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at    com.om.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NoSuchMethodException: onClick [class android.view.View]
    at java.lang.Class.getConstructorOrMethod(Class.java:472)
    at java.lang.Class.getMethod(Class.java:857)
    at android.view.View$1.onClick(View.java:3803)
        at android.view.View.performClick(View.java:4438)
        at android.view.View$PerformClick.run(View.java:18422)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)

谁能告诉我是什么问题? 先谢谢了。

Could anyone tell me what the problem is? Thanks in advance.

推荐答案

既然你说这是在 AndroidFileDownload ,我建议你做如下:

Since you said it's in AndroidFileDownload, I'd suggest you to do as follows:

@Override
public void onClick(View view)
     // you need to get the id of the clicked view:
    if(view.getId() == R.id.download_button) {
        EditText urlInputField = (EditText) thisActivity.findViewById(R.id.url_input);
        String urlInput = urlInputField.getText().toString();
        downloaderThread = new DownloaderThread(thisActivity, urlInput);
        downloaderThread.start();
    }
}

这是原因: NoSuchMethodException:的onClick [类android.view.View] 。这意味着,你的的onClick 方法找不到您的按钮( ID ),您也设置一个 onClickListener 方法。

This is the cause of: NoSuchMethodException: onClick [class android.view.View]. It means that your onClick method cannot find your button (id) where you did set an onClickListener method.

然后,在未来,我认为这个问题将是 AndroidFileDownload 未在清单文件中声明。你应该如下添加:

And then, in the future, I think the problem will be that AndroidFileDownload is not declared in your Manifest file. You should add it as follows:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.NautGames.xecta.app.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    // add every activity in your app like this below
    <activity
        android:name="com.NautGames.xecta.app.AndroidFileDownloader"
        android:label="@string/app_name" />
</application>  

这篇关于&QUOT;不幸的是,...已经停止&QUOT;在Android的下载文件时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆