使用的Android SDK Tools版本:24.3.4
使用的Eclipse版本信息:
Version: Mars Release (4.5.0)
Build id: 20150621-1200
Eclipse插件ADT版本:23.0.6.
在Eclipse中创建一个新工程,选择Android Application Project,点击Next:
如下图所示,填入Application Name,ProjectName,Package Name,下方的选项则是自动的:
接下来的默认选项则如下图所示:
第一个Create custom launcher icon是修改应用程序图标,如图选中,则Next出现下面对话框,不修改图标则直接Next跳过:
接下来是生成Activery的选项,这里就选择Blank Activiry:
接下来就是设置Activery的名字和对应的Layout文件的名字,这里也使用默认的:
点击Finish后,生成项目文件:
发现自动生成了一个appcompat_v7的Library工程,它是一个兼容低版本的支持库,关于Android Support Library请参见:http://blog.csdn.net/crazybigfish/article/details/18363351
选中activity_main.xml文件,在右则发现无法可视化显示,下方还有错误提示:
java.lang.NullPointerException
Exception details are logged in Window > Show View > Error Log
The following classes could not be instantiated:
- android.support.v7.internal.app.WindowDecorActionBar (Open Class, Show Error Log)
- android.support.v7.internal.widget.ActionBarContextView (Open Class, Show Error Log)
- android.support.v7.widget.Toolbar (Open Class, Show Error Log)
See the Error Log (Window > Show View) for more details.
因为创建工程时的编译SDK选的是版本22,将右则的版本选择换为21就可以正常显示了,如果遇到类似情况,可以换到更低的版本:
连上手机调试运行,可以各类手机助手自动安装手机驱动,记得关掉手机助手后再到Eclipse中进行调试运行,在工程右键菜单中选择Debug as\Android Application:
在设备列表中选择已成功连接的手机,将会自动将应用安装到手机并运行:
在手机上运行后如下图,应用界面上方有个大的标题”Demo1":
在工程的源代码目录Src中找到MainActivity.java打开:
package maxwoods.demo1;
import android.support.v7.app.ActionBarActivity;
import maxwoods.demo1.R;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
发现其引入了android.support.v7.app.ActionBarActivity,MainActivity继承自ActionBarActivity,而且ActionBarActivity还被标记为过期,可以将它改为AppCompatActivity。这里为了去掉ActionBar,现在修改MainActivity继承自Activity,并加上import android.app.Activity:
package maxwoods.demo1;
import maxwoods.demo1.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
重新运行程序,ActionBar没有了: