简体   繁体   中英

App crashes when Android Studio hits breakpoint which is inside a proxy

In my daily android development, I've seen several times of app crash when debugging with android studio. I thought it was the problem of android studio. Today I tried debugging using jdb, but the app still crashed when I typed 'where' command. And the backtrace stopped at something like invoke proxy, which is not as usual. Usually, at the top of the backtrace is something like handleMessage/nativePollOnce. So I started thinking maybe it is caused by the proxy. So I wrote some simple code with Proxy to verify it. And the app crashed on the breakpoint. Has anybody seen this before? How can I make the app not crash? It is really annoying.

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.activity_main);
    bindProxy(new Runnable() {
        @Override
        public void run() {
            setContentView(R.layout.activity_main); // Set breakpoint here
        }
    }).run();
}

Runnable bindProxy(final Runnable r) {
    return (Runnable)Proxy.newProxyInstance(getClassLoader(), new Class[]{Runnable.class}, new InvocationHandler() {
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("Wrapped runnable");
            r.run();
            return null;
        }
    });
}
}

jdb backtrace:

Breakpoint hit: "thread=main", android.support.v7.app.AppCompatActivity.setContentView(), line=139 bci=0

main[1] where
  [1] android.support.v7.app.AppCompatActivity.setContentView (AppCompatActivity.java:139)
  [2] com.example.ally.ipc.MainActivity$1.run (MainActivity.java:32)
  [3] com.example.ally.ipc.MainActivity$2.invoke (MainActivity.java:44)
  [4] java.lang.reflect.Proxy.invoke (Proxy.java:913)

The application has been disconnected

for what you need this Runnable , which is touching a View , which it does not own? you have to inflate the content-view and do not touch views from outside of their owner's scope. just alike this:

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

this is because MainActivity should be the owner of the View ; a Runnable does not even run on the UIThread - which hopefully should make it understandable, why this would never work out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM