首页 \ 问答 \ 这个Log.d有什么问题(Whats wrong with this Log.d)

这个Log.d有什么问题(Whats wrong with this Log.d)

如果我对它进行评论,那么Log.d()有什么问题,然后没有错误显示,但是显示了Log.d()值,但也得到了FATAL EXCEPTION:AsyncTask#1和app崩溃。

包com.sha.okhttp;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

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

        new AsyncTask<Void,Void,String>() {

            @Override
            protected String doInBackground(Void... params) {

                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url("https://api.github.com/users/mdshareque")
                        .build();

                try {
                    Response response = client.newCall(request).execute();
                    Log.d(TAG, "doInBackground() called with: " + "params = [" + response.body().string() + "]");
                    return response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                return null;
            }
        }.execute();


    }
}

日志

05-06 11:53:47.346 16677-16677/? I/art: Not late-enabling -Xcheck:jni (already on)
05-06 11:53:47.346 16677-16677/? W/art: Unexpected CPU variant for X86 using defaults: x86
05-06 11:53:47.366 16677-16684/? E/art: Failed sending reply to debugger: Broken pipe
05-06 11:53:47.368 16677-16684/? I/art: Debugger is no longer active
05-06 11:53:47.368 16677-16684/? I/art: Starting a blocking GC Instrumentation
05-06 11:53:47.472 16677-16677/? W/System: ClassLoader referenced unknown path: /data/app/com.sha.okhttp-1/lib/x86
05-06 11:53:47.478 16677-16677/? I/InstantRun: starting instant run server: is main process
05-06 11:53:47.479 16677-16677/? V/InstantRun: Starting server socket listening for package com.sha.okhttp on android.net.LocalSocketAddress@2b08a24
05-06 11:53:47.480 16677-16677/? V/InstantRun: Started server for package com.sha.okhttp
05-06 11:53:47.551 16677-16677/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
05-06 11:53:47.761 16677-16701/? D/NetworkSecurityConfig: No Network Security Config specified, using platform default
05-06 11:53:47.819 16677-16702/? I/OpenGLRenderer: Initialized EGL, version 1.4
05-06 11:53:47.819 16677-16702/? D/OpenGLRenderer: Swap behavior 1
05-06 11:53:47.858 16677-16677/? W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
05-06 11:53:49.398 16677-16701/com.sha.okhttp D/MainActivity: doInBackground() called with: params = [{"login":"mdshareque","id":17742511,"avatar_url":"https://avatars1.githubusercontent.com/u/17742511?v=3","gravatar_id":"","url":"https://api.github.com/users/mdshareque","html_url":"https://github.com/mdshareque","followers_url":"https://api.github.com/users/mdshareque/followers","following_url":"https://api.github.com/users/mdshareque/following{/other_user}","gists_url":"https://api.github.com/users/mdshareque/gists{/gist_id}","starred_url":"https://api.github.com/users/mdshareque/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mdshareque/subscriptions","organizations_url":"https://api.github.com/users/mdshareque/orgs","repos_url":"https://api.github.com/users/mdshareque/repos","events_url":"https://api.github.com/users/mdshareque/events{/privacy}","received_events_url":"https://api.github.com/users/mdshareque/received_events","type":"User","site_admin":false,"name":"Shareque","company":null,"blog":"","location":null,"email":null,"hireable":true,"bio":null,"public_repos":3,"public_gists":0,"followers":0,"following":1,"created_at":"2016-03-09T11:18:48Z","updated_at":"2017-05-03T05:01:40Z"}]

                                                              --------- beginning of crash
05-06 11:53:49.400 16677-16701/com.sha.okhttp E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                                Process: com.sha.okhttp, PID: 16677
                                                                java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                    at android.os.AsyncTask$3.done(AsyncTask.java:325)
                                                                    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                    at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
                                                                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                    at java.lang.Thread.run(Thread.java:761)
                                                                 Caused by: java.lang.IllegalStateException: closed
                                                                    at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:397)
                                                                    at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:391)
                                                                    at okhttp3.internal.Util.bomAwareCharset(Util.java:431)
                                                                    at okhttp3.ResponseBody.string(ResponseBody.java:173)
                                                                    at com.sha.okhttp.MainActivity$1.doInBackground(MainActivity.java:36)
                                                                    at com.sha.okhttp.MainActivity$1.doInBackground(MainActivity.java:23)
                                                                    at android.os.AsyncTask$2.call(AsyncTask.java:305)
                                                                    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
                                                                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
                                                                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
                                                                    at java.lang.Thread.run(Thread.java:761) 

有什么遗失的吗?


What's wrong with Log.d() if I comment it, then no error shows, but with Log.d() values display but also get FATAL EXCEPTION: AsyncTask #1 and app crash.

package com.sha.okhttp;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

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

        new AsyncTask<Void,Void,String>() {

            @Override
            protected String doInBackground(Void... params) {

                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url("https://api.github.com/users/mdshareque")
                        .build();

                try {
                    Response response = client.newCall(request).execute();
                    Log.d(TAG, "doInBackground() called with: " + "params = [" + response.body().string() + "]");
                    return response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                return null;
            }
        }.execute();


    }
}

Log

05-06 11:53:47.346 16677-16677/? I/art: Not late-enabling -Xcheck:jni (already on)
05-06 11:53:47.346 16677-16677/? W/art: Unexpected CPU variant for X86 using defaults: x86
05-06 11:53:47.366 16677-16684/? E/art: Failed sending reply to debugger: Broken pipe
05-06 11:53:47.368 16677-16684/? I/art: Debugger is no longer active
05-06 11:53:47.368 16677-16684/? I/art: Starting a blocking GC Instrumentation
05-06 11:53:47.472 16677-16677/? W/System: ClassLoader referenced unknown path: /data/app/com.sha.okhttp-1/lib/x86
05-06 11:53:47.478 16677-16677/? I/InstantRun: starting instant run server: is main process
05-06 11:53:47.479 16677-16677/? V/InstantRun: Starting server socket listening for package com.sha.okhttp on android.net.LocalSocketAddress@2b08a24
05-06 11:53:47.480 16677-16677/? V/InstantRun: Started server for package com.sha.okhttp
05-06 11:53:47.551 16677-16677/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
05-06 11:53:47.761 16677-16701/? D/NetworkSecurityConfig: No Network Security Config specified, using platform default
05-06 11:53:47.819 16677-16702/? I/OpenGLRenderer: Initialized EGL, version 1.4
05-06 11:53:47.819 16677-16702/? D/OpenGLRenderer: Swap behavior 1
05-06 11:53:47.858 16677-16677/? W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
05-06 11:53:49.398 16677-16701/com.sha.okhttp D/MainActivity: doInBackground() called with: params = [{"login":"mdshareque","id":17742511,"avatar_url":"https://avatars1.githubusercontent.com/u/17742511?v=3","gravatar_id":"","url":"https://api.github.com/users/mdshareque","html_url":"https://github.com/mdshareque","followers_url":"https://api.github.com/users/mdshareque/followers","following_url":"https://api.github.com/users/mdshareque/following{/other_user}","gists_url":"https://api.github.com/users/mdshareque/gists{/gist_id}","starred_url":"https://api.github.com/users/mdshareque/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mdshareque/subscriptions","organizations_url":"https://api.github.com/users/mdshareque/orgs","repos_url":"https://api.github.com/users/mdshareque/repos","events_url":"https://api.github.com/users/mdshareque/events{/privacy}","received_events_url":"https://api.github.com/users/mdshareque/received_events","type":"User","site_admin":false,"name":"Shareque","company":null,"blog":"","location":null,"email":null,"hireable":true,"bio":null,"public_repos":3,"public_gists":0,"followers":0,"following":1,"created_at":"2016-03-09T11:18:48Z","updated_at":"2017-05-03T05:01:40Z"}]

                                                              --------- beginning of crash
05-06 11:53:49.400 16677-16701/com.sha.okhttp E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                                Process: com.sha.okhttp, PID: 16677
                                                                java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                    at android.os.AsyncTask$3.done(AsyncTask.java:325)
                                                                    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                    at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
                                                                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                    at java.lang.Thread.run(Thread.java:761)
                                                                 Caused by: java.lang.IllegalStateException: closed
                                                                    at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:397)
                                                                    at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:391)
                                                                    at okhttp3.internal.Util.bomAwareCharset(Util.java:431)
                                                                    at okhttp3.ResponseBody.string(ResponseBody.java:173)
                                                                    at com.sha.okhttp.MainActivity$1.doInBackground(MainActivity.java:36)
                                                                    at com.sha.okhttp.MainActivity$1.doInBackground(MainActivity.java:23)
                                                                    at android.os.AsyncTask$2.call(AsyncTask.java:305)
                                                                    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
                                                                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
                                                                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
                                                                    at java.lang.Thread.run(Thread.java:761) 

Is there anything which is missing


原文:https://stackoverflow.com/questions/43817536
更新时间:2023-12-05 09:12

最满意答案

在C ++中NULL的值相当于0 ,而0是一个完美有效的char const*常量。 字符串类有一个非explicit构造函数,它使用char const* ,即编译器将编译代码,但它不起作用:即,虽然编译器除了代码,但它是未定义的行为。

看看C ++ 11中的定义,我没有看到使用std::nullptr_tdelete d重载,也就是说,它也会在从nullptr转换时编译。 我本来希望会有一个delete d重载,使用std::nullptr_t ,这将允许编译器在传递nullptr时至少捕获这种情况。


The value NULL is equivalent to 0 in C++ and 0 is a perfectly valid char const* constant. The string class has a non-explicit constructor taking char const*, i.e. the compiler will compile the code but it won't work: that is, although the compiler excepts the code, it is undefined behavior.

Looking at the definition in C++11 I don't see a deleted overload taking a std::nullptr_t, i.e., it will also compile when converting from a nullptr. I would have hoped that there would be a deleted overload taking a std::nullptr_t which would allow the compiler to catch the situation, at least, when passing nullptr.

相关问答

更多
  • 以下任何一项都可以工作: return std::unique_ptr{}; return std::unique_ptr(nullptr); 要测试返回的对象是否指向有效的对象,只需使用: if ( returnedData ) { // ... } 请参阅http://en.cppreference.com/w/cpp/memory/unique_ptr/operator_bool 。 Either of the following should work: ...
  • 在C ++中NULL的值相当于0 ,而0是一个完美有效的char const*常量。 字符串类有一个非explicit构造函数,它使用char const* ,即编译器将编译代码,但它不起作用:即,虽然编译器除了代码,但它是未定义的行为。 看看C ++ 11中的定义,我没有看到使用std::nullptr_t的delete d重载,也就是说,它也会在从nullptr转换时编译。 我本来希望会有一个delete d重载,使用std::nullptr_t ,这将允许编译器在传递nullptr时至少捕获这种情况。 ...
  • C ++引用不能为null。 如果要返回对生命周期与函数调用范围无关的对象的引用(如数据成员),则可以安全地返回原始指针(我建议使用指向const的指针 )。 std::string const* foo::bar() const { if (condition) { return &some_data_member; } else { return nullptr; } } 如果没有,最好的解决方案是使用包装类型,如boost::optional ...
  • 在C ++ 11之前,没有要求std::string (或模板类std::basic_string - 其中std :: string是一个实例)存储尾部'\0' 。 这反映在data()和c_str()成员函数的不同规范中c_str()返回一个指向底层数据的指针(它不需要以'\0'结尾并且c_str()终止'\0'但是,同样地,没有要求在内部不存储结尾的'\0' (在存储数据末尾访问字符是未定义的行为).....并且为了简单起见,一些无论如何,实现选择追加尾随'\0' 。 随着C ++ 11,这改变了。 ...
  • std::string支持嵌入的NUL字符* 。 您的示例代码不会产生预期结果的事实是,因为您正在从指向零终止字符串的指针构造std::string string。 没有长度信息,c'tor停在第一个NUL角色。 s包含Hello ,因此是输出。 如果你想构造一个带有嵌入NUL字符的std::string ,你必须使用一个带有明确长度参数的c'tor : std::string s("Hello\0, World", 13); std::cout << s << std::endl; 产生这个输出 : H ...
  • 我过去必须处理这个问题,我只是从SWIG中获取了std::string转换模板,并在从std::string和std::string创建Python字符串时使用PyString_FromStringAndSize了一些调整。以另一种方式接受size参数的构造函数。 那是一个非常旧的SWIG版本 - 我认为新版本中的内置转换模板已经更新,可以自动完成。 这个问题是否可能出现在C ++方面? (例如,在Mark Tolonen的示例中,第一个示例由于在没有size参数的构造函数调用中嵌入NULL而被截断)。 I ...
  • 这里实际发生的是, NULL被解释为char const* ,并试图转换为std::string (你可以将char*类型的C字符串转换为STL字符串,对吧?)。 为了处理它,你可以添加另一个构造函数 class myString { public: myString(const std::string& str) : _str(str) {} myString(const char* str) : _str(str==NULL?"":std::string(str)) { /* ...
  • 您正在将std::string传递给该函数,但它期望以null结尾的const char * 。 std::string::c_str()成员函数可用于获取一个: rb_str_new_cstr(string.c_str()); You are passing std::string to the function, but it expects a null-terminated const char *. The std::string::c_str() member function can be ...
  • 在C ++ 11之前,在调用c_str()之前,std :: string不需要包含尾随的nul。 http://en.cppreference.com/w/cpp/string/basic_string/data 的std :: string ::数据() 返回指向作为字符存储的基础数组的指针。 指针是范围[data(); data()+ size())有效,其中的值对应于存储在字符串中的值。 返回的数组不需要以null结尾。 如果empty()返回true,则指针是非空指针,不应取消引用。 (直到c + ...
  • 这里使用的==实际上是: template< class CharT, class traits, class Alloc > bool operator==( const basic_string& lhs, const basic_string& rhs ); 用户定义的转换序列不考虑模板类型推导,因此不能在此处(或其他)推导出CharT参数。 要解决此问题,您可能必须定义自己的非模板opera ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)