我有一个问题。 这段代码有什么问题?我点击下载按钮应用程序秋天。我不知道我是按照这里的说明做的: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 Manifest
<?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 :(得分:1)
您正在onCreate中创建一个EditText。
您声明了一个全局的EditText,但是您正在OnCreate上初始化一个新的EditText。执行下载方法时,您将从全局获取空指针。
这样做:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.button = (Button) findViewById(R.id.button);
this.editText = (EditText) findViewById(R.id.editText);