常见错误而写的Andr​​oid项目 [英] Common Errors while writing Android Project

查看:163
本文介绍了常见错误而写的Andr​​oid项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是应该被照顾,当工作在Android项目的常见错误?

这些可

  • 在Android的特定错误
  • 在Java的特定错误
解决方案

我将列出一些错误我总是得到。

文章 -

前10名的Java错误
  • 空指针错误

    1. 当我使用未初始化的变量或者我们正在创建的对象。 (爪哇)
    2. 当我们使用一些布局了看法,是不是在XML我们在上下文中设置。(安卓)


  • ClassCast异常
    • 当程序试图投对象,以一种与它不兼容。 (例如:当我尝试使用该声明为XML布局相对布局的线性布局)。


  • 的StackOverflowError
    • 这也可以发生在正确写入(但深深的递归)的程序。(Java和Android的)
    • 当程序变得无限递归。
    • 我们创建的布局(深刻而复杂的)超过了栈平台或虚拟机。递归或过多的布局将在Android中创建堆栈溢出错误
    • 有太多的内部布局。


  • ActivityNotFoundException :无法找到明确的活动类异常

    • 在该活动并非在清单中声明。


  • 机器人SecurityException异常
    • 您需要声明的应用程序清单,你的应用程序的所有许可 检查此链接 (互联网,接入联系,GPS,WIFI状态下,写入SD卡等)。


  • 的OutOfMemoryError
    • 当进行对存储器的请求时,不能使用可用的平台资源满足。主要使用位图,画廊等。


  • 应用程序无响应(ANR)

    • 主要是当你在做网络功能,或者一些长期的过程来。
      

    这将阻止UI线程,这样用户就无法做任何工作。为了避免 ANR 阅读本和放大器; <一href="http://stackoverflow.com/questions/5513457/anr-keydispatchingtimedout-error/5513623#5513623">this

这事儿我主要是让在创建Android项目。

  • 尝试使用 尝试 - 捕捉 阻止程序的所有地方。 不要留下您的catch块空,因为这可以掩盖错误:

是:

 尝试{
    //试一下
  }赶上(例外五){
      Log.e(TAG,异常的尝试捕捉,E);
      返回false;
  }
  返回true;
 

没有

 尝试{
    //试一下
  }赶上(例外五){
    返回false;
  }
  返回true;
 


使用合适的命名转换的所有变量和ID在布局。*


我读<一href="http://android-in-action.com/index.php?post/Common-errors-and-bugs-and-how-to-solve-avoid-them">one文章净它包含了一些错误,现在我加入了阿洛斯如果有冗余,请原谅我。

问题: 我的previously不错RelativeLayout的是使一个丑陋的堆或一些元素是不可见的了...这是怎么回事???我刚搬来的元素在里面? 解 : 千万不要忘记,在一个RelativeLayout的,元素被引用,并放置在相对于他们的邻居。也许有一些错误在你的元素之间的关系的层次结构。尝试打开Eclipse中的大纲视图,并单击每个元素,看看那里有一个破裂。

问题: 循环依赖不能RelativeLayout的存在 解 : 你可能已经写了相同的依赖在两个不同的方式。例如一个ImageView的是通过属性android:layout_toRightOf一个TextView和TextView中有机器人:layout_toLeftOf ImageView的。其中只有一个是必要的

问题: 我写了一个样式我的看法/布局之一,但是当我把它应用在我的XML,我在Eclipse中的布局浏览器不显示 解 : 不幸的是,这似乎是在Android ADT的一个bug,我报告,但没有任何消息为止。无论如何,没有出现恐慌,样式运作良好,但在Eclipse中,他们都无法正常显示。刚刚建立的应用程序,并启动它在仿真器或手机,你会看到,如果一切正常与否。

问题: 吐司编写正确,但不显示任何内容 解 : 这是采用了常见的错误:只需添加.show()方法来显示吐司,看看它是否工作良好。

问题: 我试图显示来自strings.xml中的字符串,但我只是有一些像0x7f060001 解 : 这是不是一个错误,只是一个显示器,由于Android的方式处理资源。当你想获取的资源,你必须使用类似的getString(R.id.something),getDrawable,方法......否则,你只是显示写成R类参考

问题: 在code一些变化没有在应用程序的任何影响 解 : 有2个选择,要么你已经忘记了敬酒类似的.show(),或仿真器无法正常更新你的应用程序。在这种情况下,你必须检查选项Eclipse中的模拟器的启动配置的清除用户数据。

问题: 如何显示在我的表格边框? 解 : 有没有直接的方法来做到这一点在Android中,你必须使用一个技巧:HTTP://www.droidnova.com/display-borders-in-tablelayout,112.html

问题: 模拟器是写在japaneese withtout您已经更改了任何参数 解 : 这有时会发生,很容易解决,只需长按任意的EditText领域,并更改输入类型你想要什么

问题: 我不能让上下文菜单出现在模拟器 解 : 长按模拟器上似乎并没有对每一种观点注册,你必须按下按钮,在4箭头的定向的中心

问题: 我下面一个有关地图的路线教程,但我不能得到它的工作,机器人没有找到包 解 : 你可能已经下了1.5 SDK编写的教程。此时,有一包显示在机器人路由,但除去在下一SDK和不再可用。它只是不可能的了。似乎有与KML文件,但没有正式的

一招

问题: 发送坐标到模拟器提供了错误的位置 解 : 确保你写的坐标一样51.16548而不是51,16548也不5116548

问题: 只有创建一个视图层次可以触摸其观点原来的线程。 解 : 你必须尝试更新从另一个线程比UI线程的视图内容。 2选项来修补这一点:无论是在你的UI线程创建一个处理程序,并发表您的Runnable来此处理或使用方法runOnUIThread运行的code正在做更新的行

问题: 访问本地主机127.0.0.1不工作 解 : 它的工作原理,你只是不这样做是正确的方法:使用10.0.2.2

What are the Common Errors which should be taken care of, while working on Android project?

These can be

  • Android Specific Error
  • Java Specific Error

解决方案

I will list some errors i m always getting.

Article - Top 10 Java Errors

  • NullPointer error

    1. when i use un initialized variable or object we are creating. (Java)
    2. when we use some layout out view that is not in xml what we set in context.(Android)


  • ClassCast Exception
    • when a program attempts to cast a an object to a type with which it is not compatible. (eg: when i try to use a linear layout which is declared as a relative layout in xml layout).


  • StackOverflowError
    • it can also occur in correctly written (but deeply recursive) programs.(java and android)
    • when a program becomes infinitely recursive.
    • we create layout (deep and complex) that exceeds that stack of platform or virtual machine . recursive or too much of layout will create Stack overflow error in Android
    • Too many inner layouts.


  • ActivityNotFoundException: Unable to find explicit activity class exception

    • The activity is not declared in manifest.


  • Android securityException
    • You need to declare all permission in the application Manifest that your application check this link (internet, access to contact,gps,wifi state,write to SDCard, etc).


  • OutofMemoryError
    • when a request for memory is made that can not be satisfied using the available platform resources . mainly using bit map, gallery , etc.


  • Application Not Responding (ANR)

    • Mainly comes when you are making network function,or some long process.

    this will block UI Thread so user can not do any work. to avoid ANR read this & this

This are thing i mainly get while creating Android Project.

  • Try to use Try - Catch block in All Place of program. Dont leave your catch block empty as this can hide errors:

Yes:

 try{
    // try something
  } catch (Exception e) {
      Log.e("TAG", "Exception in try catch", e);
      return false;
  }
  return true;

No:

  try{
    // try something
  } catch (Exception e) {
    return false;
  }
  return true;


Use proper Naming conversion for all variable and ID's in Layout.*


I read one article from net it contains some error now i am adding that alos if it have redundancy please forgive me.

Issue : My previously nice RelativeLayout is making an ugly heap or some elements aren't visible anymore...What's going on ??? I just moved an element in it... Solution : Never forget that in a RelativeLayout, elements are referenced and placed in relation to their neighbours. Maybe there is something wrong in the hierarchy of relationship between your element. Try opening the outline view in Eclipse and clicking each element to see where there is a rupture.

Issue : Circular dependencies cannot exist in RelativeLayout Solution : You have probably written the same dependency in two different way. For instance an ImageView as the attribute android:layout_toRightOf a TextView and the TextView has android:layout_toLeftOf the ImageView. Only one of them is necessary

Issue : I wrote a style for one of my view/layout, but when I apply it in my xml, I have no display in the layout viewer of Eclipse Solution : Unfortunately, this seems to be a bug of the android ADT, I reported it but no news so far. Anyway, no panic, styles are working well, but they aren't displayed properly in Eclipse. Just build the app and launch it on the emulator or phone and you will see if everything is fine or not.

Issue : Toast is written properly but nothing is displayed Solution : This is a common error of use : just add the .show() method to show the Toast and see if it is working well

Issue : I tried to display a String from strings.xml but I just had a number like 0x7f060001 Solution : This is not a bug, just a display due to the way android deals with resources. When you want to retrieve a resource, you have to use a method like getString(R.id.something), getDrawable, …Otherwise, you just display the reference written in the R class

Issue : Some change in code doesn't have any effect in the application Solution : there are 2 options, either you have forgotten something like the .show() of the Toast, or the emulator is not updating properly your application. In that case, you have to check the option "Wipe user Data" in your launch configuration of the emulator in Eclipse.

Issue : How to display borders on my table? Solution : There is no direct way to do that in android, you have to use a trick :http://www.droidnova.com/display-borders-in-tablelayout,112.html

Issue : the emulator is writing in japaneese withtout you having changed any parameter Solution : This happens sometimes, quite easy to fix, just long click in any EditText field, and change the input type to what you want

Issue : I can't get the Context Menu to appear in the emulator Solution : long click on emulator does not seem to register on every kind of view, you have to push the button in the center of the 4 directionnal arrows

Issue : I'm following a tutorial about map route but I can't get it work, android does not find a package Solution : You might have been following a tutorial written for 1.5 SDK. At this time, there was a package to display route in android, but it was removed in the next SDK and is not available anymore. It just not possible anymore. There seems to be a trick with KML files but nothing official

Issue : Sending coordinates to the emulator gives wrong position Solution : ensure that you wrote the coordinate like 51.16548 and not 51,16548 nor 5116548

Issue : Only the original thread that created a view hierarchy can touch its views. Solution : You must have tried to update a view content from another thread than the UI thread. 2 options to patch this : either create a handler in your UI thread and post your Runnable to this handler OR use the method runOnUIThread to run the lines of code that are doing the update

Issue : accessing localhost 127.0.0.1 doesn't work Solution : it works, you are just not doing it the right way : use 10.0.2.2

这篇关于常见错误而写的Andr​​oid项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆