如何修复异常无法在android中启动活动ComponentInfo

时间:2014-10-01 05:54:33

标签: android

 E/AndroidRuntime(1844): FATAL EXCEPTION: main
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chatr/com.example.chatr.MainActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
    at android.app.ActivityThread.access$600(ActivityThread.java:165)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5391)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
    at com.example.chatr.MainActivity.SendMessage(MainActivity.java:68)
    at com.example.chatr.MainActivity.onCreate(MainActivity.java:27)
    at android.app.Activity.performCreate(Activity.java:5122)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1150)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315)
    ... 11 more

Java代码:

public class MainActivity extends Activity {
    HubConnection conn;
    HubProxy proxy;
    EditText edittext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //

     TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            edittext = (EditText) findViewById(R.id.setIdEditText);
            new a().execute();
            microsoft.aspnet.signalr.client.Logger logger = new microsoft.aspnet.signalr.client.Logger() {

                @Override
                public void log(String message, LogLevel level) {
                    // TODO Auto-generated method stub

                }
            };
            conn = new HubConnection("http://www.signalr.lociiapp.com/",
                    "userId=" + 5, true, logger);
            proxy = conn.createHubProxy("ChatHub");

            conn.start().done(new Action<Void>() {

                @Override
                public void run(Void obj) throws Exception {
                    // TODO Auto-generated method stub
                    runOnUiThread(new Runnable() {
                        public void run() {
                            edittext.setText("Conected");
                        }
                    });
                }
            });
            SendMessage(5, 4, "Hello");
        }

        class a extends AsyncTask<Void, Void, Void> {

            @Override
            protected Void doInBackground(Void... params) {
                // TODO Auto-generated method stub
                SendMessage(5, 4, "Hello");
                return null;
            }

        }

        public void SendMessage(int id, int toid, String message) {

            proxy.invoke("SendMessage", new ReceiveObject(id, toid, message)).done(
                    new Action<Void>() {
                        @Override
                        public void run(Void obj) throws Exception {
                            runOnUiThread(new Runnable() {
                                public void run() {

                                }
                            });
                        }
                    });
        }
    }

这是我的代码我得到了异常,我有打印那里即使我有线程启动服务请检查我的代码在哪里做错了我在第68和27行得到例外:

new a().execute();proxy.invoke("SendMessage", new ReceiveObject(id, toid, message)).done(请帮帮我

1 个答案:

答案 0 :(得分:0)

代理在该点(第68行)将为null,因为您在下面初始化它并在此之前调用您的asynctask。

    public void SendMessage(int id, int toid, String message) {
        //proxy will be null as it is initialized below in onCreate()
        proxy.invoke("SendMessage", new ReceiveObject(id, toid, message)).done(
                new Action<Void>() {
                    @Override
                    public void run(Void obj) throws Exception {
                        runOnUiThread(new Runnable() {
                            public void run() {

                            }
                        });
                    }
                });
    }

在每次初始化后面移动new a().execute();或在您的情况下移动

proxy = conn.createHubProxy("ChatHub");