getDrawable()不起作用。无法解析方法'getDrawable()'。

时间:2016-07-10 01:50:37

标签: android android-asynctask wifi wifi-direct

我正在尝试使用两个设备之间的wifi dirict发送图像。然后接收器设备将其发送到数据库。

它给我这个错误

  

164-5196 / com.example.italiano_1313.wifidirectdemo E / AndroidRuntime:   致命异常:AsyncTask#2       处理:com.example.italiano_1313.wifidirectdemo,PID:4164       java.lang.RuntimeException:执行doInBackground()时发生错误               在android.os.AsyncTask $ 3.done(AsyncTask.java:300)               在java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)               at java.util.concurrent.FutureTask.setException(FutureTask.java:222)               在java.util.concurrent.FutureTask.run(FutureTask.java:242)               在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)               at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)               at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587)               在java.lang.Thread.run(Thread.java:818)        引发者:java.lang.NullPointerException:尝试调用虚方法'boolean   android.graphics.Bitmap.compress(android.graphics.Bitmap $ CompressFormat,   int,java.io.OutputStream)'在空对象引用上               at com.example.italiano_1313.wifidirectdemo.DeviceDetailFragment $ Encode_image.doInBackground(DeviceDetailFragment.java:183)               at com.example.italiano_1313.wifidirectdemo.DeviceDetailFragment $ Encode_image.doInBackground(DeviceDetailFragment.java:174)               在android.os.AsyncTask $ 2.call(AsyncTask.java:288)               在java.util.concurrent.FutureTask.run(FutureTask.java:237)   在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)   at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587)   在java.lang.Thread.run(Thread.java:818)

你可以看看吗

这是我为这个课程编写的代码

私有类Encode_image扩展了AsyncTask {

    @Override
    protected Void doInBackground(Void... voids) {


        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

        byte[] array = stream.toByteArray();
        encoded_string = Base64.encodeToString(array, 0);
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {

        makeRequest();
    }
}


public void makeRequest() {
    RequestQueue requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
    StringRequest request = new StringRequest(Request.Method.POST, "http://wam227.comli.com/connection.php",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            HashMap<String,String> map = new HashMap<>();
            map.put("encoded_string",encoded_string);
            map.put("image_name",image_name);

            return map;
        }
    };
    requestQueue.add(request);
}

/**
 * Updates the UI with device data
 *
 * @param device the device to be displayed
 */
public void showDetails(WifiP2pDevice device) {
    this.device = device;
    this.getView().setVisibility(View.VISIBLE);
    TextView view = (TextView) mContentView.findViewById(R.id.device_address);
    view.setText(device.deviceAddress);
    view = (TextView) mContentView.findViewById(R.id.device_info);
    view.setText(device.toString());
}
/**
 * Clears the UI fields after a disconnect or direct mode disable operation.
 */



public void resetViews() {
    mContentView.findViewById(R.id.btn_connect).setVisibility(View.VISIBLE);
    TextView view = (TextView) mContentView.findViewById(R.id.device_address);
    view.setText(R.string.empty);
    view = (TextView) mContentView.findViewById(R.id.device_info);
    view.setText(R.string.empty);
    view = (TextView) mContentView.findViewById(R.id.group_owner);
    view.setText(R.string.empty);
    view = (TextView) mContentView.findViewById(R.id.status_text);
    view.setText(R.string.empty);
    mContentView.findViewById(R.id.btn_start_client).setVisibility(View.GONE);
    this.getView().setVisibility(View.GONE);
}
/**
 * A simple server socket that accepts connection and writes some data on
 * the stream.
 */
public class FileServerAsyncTask extends AsyncTask<Void, Void, String> {
    private Context context;
    private TextView statusText;
    /**
     * @param context
     * @param statusText
     */
    public FileServerAsyncTask(Context context, View statusText) {
        this.context = context;
        this.statusText = (TextView) statusText;
    }
    @Override
    protected String doInBackground(Void... params) {
        try {
            ServerSocket serverSocket = new ServerSocket(8988);
            Log.d(WiFiDirectActivity.TAG, "Server: Socket opened");
            Socket client = serverSocket.accept();
            Log.d(WiFiDirectActivity.TAG, "Server: connection done");
            final File f = new File(Environment.getExternalStorageDirectory() + "/"
                    + context.getPackageName() + "/wifip2pshared-" + System.currentTimeMillis()
                    + ".jpg");
            File dirs = new File(f.getParent());
            if (!dirs.exists())
                dirs.mkdirs();
            f.createNewFile();
            Log.d(WiFiDirectActivity.TAG, "server: copying files " + f.toString());
            InputStream inputstream = client.getInputStream();
            copyFile(inputstream, new FileOutputStream(f));

            serverSocket.close();

            return f.getAbsolutePath();
        } catch (IOException e) {
            Log.e(WiFiDirectActivity.TAG, e.getMessage());
            return null;
        }
    }


    /*
     * (non-Javadoc)
     * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
     */
    @Override
    protected void onPostExecute(String result) {
        if (result != null) {
            statusText.setText("File copied - " + result);
            Intent intent = new Intent();
            intent.setAction(android.content.Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse("file://" + result), "image/*");
            context.startActivity(intent);

            bitmap =((BitmapDrawable) Uri.parse("file://" + result).getDrawable()).getBitmap();
            new Encode_image().execute();



        }

    }
    /*
     * (non-Javadoc)
     * @see android.os.AsyncTask#onPreExecute()
     */
    @Override
    protected void onPreExecute() {
        statusText.setText("Opening a server socket");
    }
}
public static boolean copyFile(InputStream inputStream, OutputStream out) {
    byte buf[] = new byte[1024];
    int len;
    try {
        while ((len = inputStream.read(buf)) != -1) {
            out.write(buf, 0, len);
        }
        out.close();
        inputStream.close();
    } catch (IOException e) {
        Log.d(WiFiDirectActivity.TAG, e.toString());
        return false;
    }
    return true;
}

}

1 个答案:

答案 0 :(得分:0)

根据日志,似乎std::array中第二行的位图为空,您应该检查其他代码。