android studio常见错误

1:android studio 新建工程出错  

Error:Execution failed for task ':app:preDexDebug'.
com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files (x86)\Java\jdk1.7.0_67\bin\java.exe'' finished with non-zero exit value 1
解决办法:
因为同时开了Eclipse,android studio,导致内存占用过多。关闭Eclipse即可。
 

2:java 类是公共的,应在名为.java 的文件中声明

 解决办法: 

“类A是公共的,应在名为A.java的文件中声明”这句话需要分两步来理解:
1、如果类A被声明为公共的(public),那么必须将类A保存在名为A.java的文件中;
2、反之,在一个文件中最多包含一个顶级的公共类,并且该公共类的名字与文件名相同。比如文件A.java中,允许定义一个或多个类,但最多允许一个顶级的公共类,此类名为A。此处强调的顶级的意思是,允许非顶级的公共类存在,如内部公共类等。

3:低版本的SDK无法运行高版本的SDK的APK

 解决办法:

  在下图1所示的build.gradle(Module:app)中,将

  minSdkVersion 9
targetSdkVersion 9
数字9改成你所需要的SDK版本对应得API版本即可(图二为SDK与API对应关系)
 

       图1:build.gradle(Module:app)

 

 

           图2:SDK与API对应关系
4:出现Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
1 protected void onCreate(Bundle savedInstanceState) {
2 
3         super.onCreate(savedInstanceState);       
4         editText1 = (EditText)findViewById(R.id.account);
5         editText2 = (EditText)findViewById(R.id.pwd);
6         button1=(Button)findViewById(R.id.loginin);
7         button2=(Button)findViewById(R.id.quit);
8         setContentView(R.layout.activity_main);
9     }
  解决:因为还没有 setContentView(R.layout.activity_main);即Activity 启动入栈后,它在屏幕最前端,就去获取 editText1 =                  (EditText)findViewById(R.id.account);,当然会报错,改成:
 1 protected void onCreate(Bundle savedInstanceState) {
 2 
 3         super.onCreate(savedInstanceState);
 4         setContentView(R.layout.activity_main);
5 editText1 = (EditText)findViewById(R.id.account); 6 editText2 = (EditText)findViewById(R.id.pwd); 7 button1=(Button)findViewById(R.id.loginin); 8 button2=(Button)findViewById(R.id.quit); 9 10 }

   即可。

5:editText1.getText().toString();即使为空,返回的也是" ".而不是NULL,不能和c混淆了。

要判断输入的数是不是HELLO,不能使用“==”来判断,需要调用equals()函数。

1 String message1 = editText1.getText().toString(); 
2 String message2 = editText2.getText().toString();
3 if(message1 != "" && message2 != "")
4 {
5     if (message1.equals("HELLO") && message2.equals("HELLO")) {
6 {...}

 

 

 

  


 

 

 

 

 

posted @ 2015-08-09 16:48  若兮_ruoxi  阅读(796)  评论(1编辑  收藏  举报