2015-03-08 53 views
-4

我有問題。 這段代碼有什麼問題?我點擊下載按鈕應用程序下降。我不知道,我這樣做是根據這裏的說明:https://www.youtube.com/watch?v=pGlqHeB5hdk&index=76&list=FLGkGbI5dUEjLideBS_TOq-w下載管理器應用程序下降

package downloader.download.downloader; 

import android.app.DownloadManager; 
import android.net.Uri; 
import android.os.Environment; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 


public class MainActivity extends ActionBarActivity { 
    Button button; 
    EditText editText; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     final Button button = (Button) findViewById(R.id.button); 
     final EditText editText = (EditText) findViewById(R.id.editText); 

     editText.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       button.setEnabled(!editText.getText().toString().trim().isEmpty()); 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 

      } 
     }); 


    } 


    public void download(View v) 
    { 
     DownloadManager.Request request = new DownloadManager.Request(Uri.parse(editText.getText().toString())); 
request.setTitle("Downloader"); 
request.setDescription("Downloanding file!"); 
request.allowScanningByMediaScanner(); 
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION); 
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Download file"); 
    } 








    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.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(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

<EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/editText" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:hint="Link" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Download" 
     android:id="@+id/button" 
     android:layout_below="@+id/editText" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignRight="@+id/editText" 
     android:layout_alignEnd="@+id/editText" 
     android:enabled="false" 
     android:onClick="download" /> 
</RelativeLayout> 

Android清單

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="downloader.download.downloader" > 
<uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 




    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".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> 

</manifest> 

編輯: 登錄

03-08 17:51:06.055 14101-14101/downloader.download.downloader D/dalvikvm﹕ Late-enabling CheckJNI 
03-08 17:51:06.565 14101-14101/downloader.download.downloader I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted 
03-08 17:51:06.565 14101-14101/downloader.download.downloader W/dalvikvm﹕ VFY: unable to resolve virtual method 11351: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V 
03-08 17:51:06.565 14101-14101/downloader.download.downloader D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000 
03-08 17:51:06.565 14101-14101/downloader.download.downloader I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll 
03-08 17:51:06.565 14101-14101/downloader.download.downloader W/dalvikvm﹕ VFY: unable to resolve virtual method 11357: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V 
03-08 17:51:06.565 14101-14101/downloader.download.downloader D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000 
03-08 17:51:06.565 14101-14101/downloader.download.downloader I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled 
03-08 17:51:06.565 14101-14101/downloader.download.downloader W/dalvikvm﹕ VFY: unable to resolve virtual method 9045: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll()V 
03-08 17:51:06.565 14101-14101/downloader.download.downloader D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e 
03-08 17:51:06.615 14101-14101/downloader.download.downloader I/dalvikvm﹕ Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged 
03-08 17:51:06.615 14101-14101/downloader.download.downloader W/dalvikvm﹕ VFY: unable to resolve virtual method 11354: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V 
03-08 17:51:06.615 14101-14101/downloader.download.downloader D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0007 
03-08 17:51:06.625 14101-14101/downloader.download.downloader I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations 
03-08 17:51:06.625 14101-14101/downloader.download.downloader W/dalvikvm﹕ VFY: unable to resolve virtual method 370: Landroid/content/res/TypedArray;.getChangingConfigurations()I 
03-08 17:51:06.625 14101-14101/downloader.download.downloader D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002 
03-08 17:51:06.625 14101-14101/downloader.download.downloader I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType 
03-08 17:51:06.625 14101-14101/downloader.download.downloader W/dalvikvm﹕ VFY: unable to resolve virtual method 392: Landroid/content/res/TypedArray;.getType (I)I 
03-08 17:51:06.625 14101-14101/downloader.download.downloader D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002 
03-08 17:51:06.735 14101-14105/downloader.download.downloader D/dalvikvm﹕ GC_CONCURRENT freed 231K, 12% free 9513K/10759K, paused 16ms+2ms, total 52ms 
03-08 17:51:06.765 14101-14101/downloader.download.downloader D/libEGL﹕ loaded /system/lib/egl/libEGL_mali.so 
03-08 17:51:06.775 14101-14101/downloader.download.downloader D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_mali.so 
03-08 17:51:06.795 14101-14101/downloader.download.downloader D/libEGL﹕ loaded /system/lib/egl/libGLESv2_mali.so 
03-08 17:51:06.815 14101-14101/downloader.download.downloader D/OpenGLRenderer﹕ Enabling debug mode 0 
03-08 17:51:21.950 14101-14101/downloader.download.downloader W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection 
03-08 17:51:36.054 14101-14101/downloader.download.downloader D/AndroidRuntime﹕ Shutting down VM 
03-08 17:51:36.054 14101-14101/downloader.download.downloader W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x410bf2a0) 
03-08 17:51:36.054 14101-14101/downloader.download.downloader E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    java.lang.IllegalStateException: Could not execute method of the activity 
      at android.view.View$1.onClick(View.java:3660) 
      at android.view.View.performClick(View.java:4162) 
      at android.view.View$PerformClick.run(View.java:17082) 
      at android.os.Handler.handleCallback(Handler.java:615) 
      at android.os.Handler.dispatchMessage(Handler.java:92) 
      at android.os.Looper.loop(Looper.java:137) 
      at android.app.ActivityThread.main(ActivityThread.java:4867) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.reflect.InvocationTargetException 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at android.view.View$1.onClick(View.java:3655) 
            at android.view.View.performClick(View.java:4162) 
            at android.view.View$PerformClick.run(View.java:17082) 
            at android.os.Handler.handleCallback(Handler.java:615) 
            at android.os.Handler.dispatchMessage(Handler.java:92) 
            at android.os.Looper.loop(Looper.java:137) 
            at android.app.ActivityThread.main(ActivityThread.java:4867) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774) 
            at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
      at downloader.download.downloader.MainActivity.download(MainActivity.java:51) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at android.view.View$1.onClick(View.java:3655) 
            at android.view.View.performClick(View.java:4162) 
            at android.view.View$PerformClick.run(View.java:17082) 
            at android.os.Handler.handleCallback(Handler.java:615) 
            at android.os.Handler.dispatchMessage(Handler.java:92) 
            at android.os.Looper.loop(Looper.java:137) 
            at android.app.ActivityThread.main(ActivityThread.java:4867) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774) 
            at dalvik.system.NativeStart.main(Native Method) 
+0

你得到什麼錯誤?請發佈日誌 – IshRoid 2015-03-08 17:03:45

回答

1

您在onCreate中創建EditText。

您聲明瞭一個全局EditText,但是您正在OnCreate上初始化一個新EditText。當您執行下載方法時,您將從全局獲取空指針。

這樣做:

+0

下載未開始: -/ – 2015-03-09 12:30:16

+0

有一些豁免?看起來你沒有調用下載功能,只是準備了請求。嘗試:getDownloadManager()。enqueue(request); – 2015-03-09 16:24:50