代码之家  ›  专栏  ›  技术社区  ›  tpow

Android应用程序在模拟器中崩溃-日志空白

  •  0
  • tpow  · 技术社区  · 15 年前

    我对Android完全陌生,而且几乎是一个Java NeWB。

    我有一个简单的应用程序,我正在建立,以了解安卓的开发环境的窍门-以及像点击事件等。

    应用程序加载,我可以使用按钮处理程序更改文本字段中的文本。但是,当我导入location类并尝试进行简单的GPS调用时,应用程序崩溃。

    问题是,Eclipse(错误控制台)中的一切看起来都很好——在Android模拟器(devtools)中我没有看到任何异常。我打开了logcat窗口,但是我没有在eclipse/code中做任何事情来发送logcat任何东西(我需要吗?)

    有人能看出这有什么问题吗?有没有更好的故障排除方法?

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.*;
    import android.location.*;
    
    
    public class locationDisplay extends Activity {
    
        private EditText text;
        private Location GPSLocation;
        double dblLat;
        double dblong;
        String strLat;
    
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main); // bind the layout to the activity
            text = (EditText) findViewById(R.id.EditText01);
            text.setText("No button pressed");
    
        }
    
        // Handler for each button -- Button01 is when it crashes
        public void myClickHandler(View view) {
    
            switch (view.getId()) {
            case R.id.Button01: 
                dblLat = GPSLocation.getLatitude();
                strLat = Double.toString(dblLat);
                text.setText(strLat);
                break;
            case R.id.Button02:
                text.setText("Button 2 was clicked");
                break;
            case R.id.Button03:
                text.setText("Button 3 was clicked");
                break;
            }
        }
    
    3 回复  |  直到 11 年前
        1
  •  4
  •   Steve Haley    15 年前

    您不需要编写任何东西来获取logcat中的默认消息;未捕获的异常报告应该在程序崩溃时自动出现。然而,有时logcat和模拟器会断开连接,消息就会全部消失。只需关闭Eclipse和模拟器,重新启动它们,消息就会重新出现。判断链接是否已重新建立的一个简单方法是在模拟器启动期间。正如闪烁的“android”文本在花哨的字体消失,带你到锁屏,你应该看到在logcat上大约100行的文本闪烁。如果这不发生,那么logcat就不会收到它的消息。

    在Android中显示调试消息的方法是使用 Log.d("some name for your log statements so you can filter the LogCat messages", "The actual debug statement here"); . 您经常会发现人们在他们的应用程序中使用诸如静态最终字符串日志标记之类的东西,这样他们就可以确保他们的日志始终具有相同的标记,因此,过滤器不会错过任何消息。

    至于这里的实际代码,rpond是正确的,您从未初始化gpslocation对象。

        2
  •  2
  •   Robby Pond    15 年前

    gpslocation对象为空。您需要访问locationservice以获取当前位置。使用模拟器,您将需要手动发送位置。

    Location Services

        3
  •  1
  •   Chad Hedgcock    14 年前

    有时logcat会“忘记”连接并运行设备/模拟器。这似乎发生在您同时在线使用设备和模拟器,然后断开其中一个之后。如果您没有从logcat获得任何信息,请转到窗口>显示视图>其他设备,然后单击要记录的设备。