Friday, August 20, 2010

使用git同步代码

git是一个代码同步工具。
  1. 1.安装git
Ubuntu中的git-core有许多工具是没有的,所以应该下载一个git的deb包进行安装。
  1. 2.安装ssh
同步有许多方法,有http,也可以使用ssh。我使用的是ssh。
首先,安装一个ssh服务,名字叫openssh-server。安装后自动启动。
然后在客户机上使用ssh-keygen -t rsa生成一个密钥,在生成时,密码掩码要留空,这样就可自动登录。
生成一个名为id_rsa.pub(?),的文件,该 文件位于.ssh目录中,将这个文件发送到服务器的.ssh目录中,更名为authorized_keys,然后重新使用ssh登录,会发现已经不要密码了。现在准备工作作完,开始部署服务器。
  1. 3.部署git服务
首先,要在服务器端创建名为/var/cache/git/workspace.git的目录,这个是必须的。
然后要给该目录一个权限。
cd  /var/cache/git/workspace.git
chown -R root:www-data ./
而且要将当前用户加入到www-data组之中。
在该目录中生成一个git反响:
git --bare init
chmod g+rwx -R ./
  1. 4.客户端向服务器添加代码
客户端操作:
进入一个不是workspace的目录。创设是code。
执行:
会将服务器同步下来。由于服务器中并无代码,所以我们还需要加入一些代码。
(1)将代码复制到code目录。
(2)git add ./,这样就会将当前目录中的所有代码添加到git中。
(3)git commit -m 'init' ,第2步并未真正提交代码,这是提交的命令
 (4)git push origin master ,将本地作为master分支提交到服务器的origin中。这两个关键词都不能更改。这是第一次,执行完后,以后就可直接git push了。
现在,就将本地的代码同步到服务器上了。
  1. 5.从任意客户端获取代码
现在,任意客户端(安装了git的机器),进入到一个要获取代码的目录。
然后,
就会将我们上传到服务器的代码同步到本机。
然后我们也可以参照上一节的操作上传代码了。

注意:Eclipse的代码有点特别。如果要从服务器同步代码的话。那么,不能将同步来的项目代码放置在这Eclipse的项目默认目录(一般是~/workspace),否则在打开项目文件时会报错。放置在另外的其他目录就没任何问题了。
 







使用按钮打开一个activity

For example, inside the onCreateOptionsMenu() method, you can define a new menu item with an Intent like this:

  MenuItem menuItem = menu.add(0, PHOTO_PICKER_ID, 0, "Select Photo");
menuItem
.setIntent(new Intent(this, PhotoPicker.class));
  这是SDK中的一段话,意思是可以在创建菜单时使用SetIntent来使该项可打开一个Activity.
  

关于组菜单的操作

在菜单创建代码后面加上如下一句,则会使一组菜单成为单选状态。
 menu_send.setGroupCheckable(0, true, true);

代码:
      menu.setHeaderTitle(R.string.backup_conmenu);
            SubMenu menu_send=menu.addSubMenu("send");
            menu_send.add(0,menu_email,0,"email");
            menu_send.add(0,menu_wifi,0,"wifi");
            menu_send.setGroupCheckable(0, true, true);
            menu_send.setGroupEnabled(0, false);


SDK中的一句话应该如何理解:

This will display a checkbox with the menu item (unless it's in the Icon Menu). When the item is selected, the onOptionsItemSelected() callback is called as usual. It is here that you must set the state of the checkbox. You can query the current state of the item with isChecked() and set the checked state with setChecked(). Here's what this looks like inside theonOptionsItemSelected() callback:

  switch (item.getItemId()) {
case VIBRATE_SETTING_ID:
 
if (item.isChecked()) item.setChecked(false);
 
else item.setChecked(true);
 
return true;
...这里为什么要不断设置单选框的状态?
}
我在实际代码中这样:

    @Override
    public boolean onContextItemSelected(MenuItem item) {
//      AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    TextView tview3=(TextView)findViewById(R.id.hello);
     switch (item.getItemId()) {
      case menu_email:
      tview3.setText("your data will send by email");
        return true;
      case menu_wifi:
      tview3.setText("your data will send by wifi");
        return true;
      case menu_apple:
      if (item.isChecked()) item.setChecked(false);
      else item.setChecked(true);
      tview3.setText("your favorite is apple");
      return true;
      case menu_pear:
      if (item.isChecked()) item.setChecked(false);
      else item.setChecked(true);
      tview3.setText("your favorite is pear");
      return true;
      case menu_ear:
      if (item.isChecked()) item.setChecked(false);
      else item.setChecked(true);
      tview3.setText("your favorite is ear");
      
      return true;
      case menu_egg:
//      if (item.isChecked()) item.setChecked(false);
//      else item.setChecked(true);
      tview3.setText("your favorite is egg");
      return true;
      default:
        return super.onContextItemSelected(item);
      }
     }

似乎也没有什么问题。

关于菜单组,我的理解错误

菜单组的状态设置,应该是在创建菜单时设置。
代码:
            menu.setHeaderTitle(R.string.backup_conmenu);
            SubMenu menu_send=menu.addSubMenu("send");
            menu_send.add(0,menu_email,0,"email");
            menu_send.add(0,menu_wifi,0,"wifi");
            menu_send.setGroupEnabled(0, false);
这样就可以进行设置了。
思路应该是创建菜单时,根据某些条件动态设置一些菜单组为不可用,或选中状态之类。
下面一句来自SDK:
You can show or hide the entire group with setGroupVisible(); enable or disable the group withsetGroupEnabled(); and set whether the items can be checkable with setGroupCheckable().

菜单组的几个命令如何使用

Android的菜单组,有几个命令是用来控制菜单组的。但是,菜单只有在创建时才有组的概念。在其他的Activity中也可能通过组id来对该组进行设置吗?这样的话,组编号在整个软件的生命周期中是唯一了?
当使用时,却发现提示,说setGroupEnabled函数并未定义。不知是什么原因。

Wednesday, August 18, 2010

动态生成菜单的一段还末理解的代码

这段代码暂时看不太懂,先记下来。等看了intent的内容后再回头看。 
  @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        final boolean haveItems = getListAdapter().getCount() > 0;

        // If there are any notes in the list (which implies that one of
        // them is selected), then we need to generate the actions that
        // can be performed on the current selection.  This will be a combination
        // of our own specific actions along with any extensions that can be
        // found.
        if (haveItems) {
            // This is the selected item.
            Uri uri = ContentUris.withAppendedId(getIntent().getData(), getSelectedItemId());

            // Build menu...  always starts with the EDIT action...
            Intent[] specifics = new Intent[1];
            specifics[0] = new Intent(Intent.ACTION_EDIT, uri);
            MenuItem[] items = new MenuItem[1];

            // ... is followed by whatever other actions are available...
            Intent intent = new Intent(null, uri);
            intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
            menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, null, specifics, intent, 0,
                    items);

            // Give a shortcut to the edit action.
            if (items[0] != null) {
                items[0].setShortcut('1', 'e');
            }
        } else {
            menu.removeGroup(Menu.CATEGORY_ALTERNATIVE);
        }

        return true;
    }

使用xml文件创建菜单的事件处理函数

使用xml文件创建的菜单程序,在使用 swtch捕捉用户点击时,要使用形似R.id.opmen1之类的变量才可正确捕捉。
函数如下:
 public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.opmenu1:
//            newGame();
         int i=0;
            return true;
        case R.id.opmenu2:
//            quit();
         int j=1;
            return true;
        }
        return false;
    }

Friday, August 13, 2010

错误:Java Model Status [gen [****]does not exist]

在使用Eclipse的过程中,突然所有项目都出现了错误,提示是:
Java Model Status [gen [****]does not exist]
后来,将gen目录(包括其中的文件R)删除,故障排除。不知是什么原因。

网上说,也可以更换一下SDK的版本来解决这个问题。