今天维护网站时,不小心将bbs程序给删除了。

今天维护网站时,不小心将bbs程序给删除了。本来是删除另一个目录的,只是之前也将bbs所在的目录也选上了,点击删除后,一闪发现有bbs目录,为时已晚,一下全部将bbs删除了,linux删除东西真是容易!再访问网站已出现404了。这两天刚上线的网站,好在今天访问量不是很大,并且数据库还在,赶快找代码恢复,上传了代码,再次访问bbs,出现安装界面,汗。如果别人访问时也出现这个界面,往下安装就麻烦了,赶快将根目录下放一个index.html文档,说明正在维护中…. 接着一步步的安装。不久就恢复了,只是上传的附件要一个个再上传了。

Android 使用findViewById得到null

今天调试一段程序发现一个隐蔽的小问题,找了好久才找到问题。 我用layout定义了一个button. 但是在程序中无法用findViewById获得到它的引用(得到的是null)。 layout.xml定义如下 <Button id="@+pageBar/next_bt" android:layout_width="80px" android:layout_height="40px" android:text="下页"/> nextBt = (Button)findViewById(R.pageBar.next_bt); 得到的结果nextBt为null; 百思不解,最后仔细查看,与其它代码对比,终于发现一个小问题,就是id的问题。不应该使用id="@+pageBar/next_bt"(据说是android的原来的风格) 现在的风格是android:id="@+pageBar/next_bt" 修改后就可以了。 以下我在网上找了其它原因: 1、在setContentView之前调用findViewById 2、layout文件夹,layout-land和layout-port文件夹没有分清,导致修改的layout文件没有起效果 3、想要得到的xml布局中的View不在当前活动中,要使用View layout=inflater.inflate(R.layout.{布局文件}, null)得到控件所在的布局View,R.layout.{布局文件}就是你想要得到的控件所在的布局。然后利用这个布局来寻找 转载请标明出处:3G Study :http://blog.3gstdy.com/archives/43

Android用ImageView显示本地和网上的图片

ImageView是Android程序中经常用到的组件,它将一个图片显示到屏幕上。 在UI xml定义一个ImageView如下:

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.myimage);
     ImageView image1 = (ImageView) findViewById(R.myImage.image);
     //Bitmap bitmap = getLoacalBitmap("/aa/aa.jpg"); //从本地取图片
     Bitmap bitmap =
getHttpBitmap("http://blog.3gstdy.com/wp-content/themes/twentyten/images/headers/path.jpg");
//从网上取图片
     image1 .setImageBitmap(bitmap);    //设置Bitmap
}

/**
* 加载本地图片
* http://bbs.3gstdy.com
* @param url
* @return
*/
public static Bitmap getLoacalBitmap(String url) {
     try {
          FileInputStream fis = new FileInputStream(url);
          return BitmapFactory.decodeStream(fis);
     } catch (FileNotFoundException e) {
          e.printStackTrace();
          return null;
     }
}

/**
* 从服务器取图片
*http://bbs.3gstdy.com
* @param url
* @return
*/
public static Bitmap getHttpBitmap(String url) {
     URL myFileUrl = null;
     Bitmap bitmap = null;
     try {
          Log.d(TAG, url);
          myFileUrl = new URL(url);
     } catch (MalformedURLException e) {
          e.printStackTrace();
     }
     try {
          HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
          conn.setConnectTimeout(0);
          conn.setDoInput(true);
          conn.connect();
          InputStream is = conn.getInputStream();
          bitmap = BitmapFactory.decodeStream(is);
          is.close();
     } catch (IOException e) {
          e.printStackTrace();
     }
     return bitmap;
}

转载请标明出处:3G Study :http://blog.3gstdy.com/archives/38

Android多线程,让耗时的操作去后台运行吧

android程序中,会有一些耗时的操作,比如从网上抓取图片,下载文件,批量更新数据库等,这些操作对于手机而言会需要很长的时间,而应用程序界面又不能等到这些操作完成后再显示,所以要让界面各这些耗时的操作并行处理,用多线程可以解决这个问题。当然还有其它解决方案,比如用Service. 我们先作一个例子吧,大概是这样的:有一个列表,每行显示的一个图片,图片是存放在网上的。如果不用多线程,也是可以的,但是要等到所有图片下载完了才能展示出来。这种方式对用户体验很不友好,所以我们采用多线程的方式,对每一个图片开启一个线程,当其下载完数据后,在主线程中显示出来。 主Activity public class TestListActivity extends ListActivity { private ImageListAdapter imageListAdapter = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.imagelist); String[] images = {"http://image.baidu.com/image1.jpg","http://image.baidu.com/image2.jpg"}; imageListAdapter = new ImageListAdapter(getApplicationContext(), images); setListAdapter(imageListAdapter); } } 适配器 import java.util.ArrayList; import java.util.List; import android.content.Context; import android.graphics.Bitmap; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class ImageListAdapter extends BaseAdapter { private Context context; private String[] myImages = null; public ImageListAdapter(Context context, String[] myImages){ this.context = context; this.myImages = myImages; } @Override public int getCount() { if(myImages == null){ return 0; } return myImages.length; } @Override public String getItem(int position) { if(position < 0 || myImages == null || position>myImages.length){ return null; } return myImages[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View item = null; if(convertView != null){ item = convertView; } else { item = View.inflate(context, R.layout.image_item, null); } final ImageView imageView = (ImageView)item.findViewById(R.id.image); final String image = getItem(position); if(image == null){ return item; } //对每个图片地址创建一个线程, **new Thread(){ public void run(){ Message msg = new Message(); msg.what = 0; //获得图片的Bitmap对象。getBitmap省略了,就是从网上通过http下载图片然后转化成一个Bitmap Bitmap bitmap = getBitmap(image); List list = new ArrayList();//将bitmap和imageView包装成一个List传到线程外 list.add(bitmap); list.add(imageView); msg.obj = list; handler.sendMessage(msg); } }.start();** return item; } private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case 0://接到从线程内传来的图片bitmap和imageView. //这里只是将bitmap传到imageView中就行了。只所以不在线程中做是考虑到线程的安全性。 **List list = (List)msg.obj; Bitmap bitmap = (Bitmap)list.get(0); ImageView iv = (ImageView)list.get(1); iv.setImageBitmap(bitmap);** break; default: super.handleMessage(msg); } } }; } 布局xml imagelist.xml image_item.xml 转载请标明出处:3G Study :http://blog.3gstdy.com/archives/27

Android模拟器连接本地服务器

在开发Android应用时,有时会要连接本地服务器,我们可能会首先想到用loaclhost或127.0.0.1等。这是不对了。因为模拟器是一个相对独立的系统,在模拟器中运行的程序用loacalhost或127.0.0.1将会连接模拟器本身。而不是你的本地计算机。 如果想要在模拟器中连接本地计算机可使用10.0.2.2这个IP. 转载请标明出处:3G Study :http://blog.3gstdy.com/archives/11

Android 给Button加个监听

Android开发过程中,Button是常用的控件,用起来也很简单,你可以在界面xml描述文档中定义,也可以在程序中创建后加入到界面中,其效果都是一样的。不过最好是在xml文档中定义,因为一旦界面要改变是话,直接修改一下xml就行了,不用修改Java程序,并且在xml中定义层次分明,一目了然。另一个是如果在程序中定义,还要将其加入到界面中,有的还要设置高度宽度,样式之类的,会使程序变得臃肿,开发和维护都不方便。 我们先在程序中定义一个Button Button button = new Button(this);//定义一个button,其中this是上下文,这段代码是在一个Activity的onCreate中创建的。 button.setWidth(100);//一定要设置宽和高。不然会出错的。 button.setHeight(50); button.setText(“Click me”);//按钮上的文字 RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.buttonLayout); relativeLayout.addView(button);//加到界面中 以下是在UI xml中定义的按钮。 接下来是要给按钮加一个监听了,就是响应点击按钮的事件。这个是在程序中完成了, button.setOnClickListener(new OnClickListener(){ public void onClick(View v) { Toast toast = Toast.makeText(getApplicationContext(), “I am Clicked”, Toast.LENGTH_LONG);//提示被点击了 toast.show(); } }); 好了,按钮就是这么简单。 转载请标明出处:3G Study :http://blog.3gstdy.com/archives/4

action不能往jsp中传递数据的问题

struts中一个action的配置如下: action 中部分源代码: public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { log.debug(“list library “); List list = libraryManagerService.listLibrarys(); if(list == null) log.debug(“there is not any library ,because list is null”); else log.debug(“there are “ + list.size() + “ librarys .”); request.setAttribute(“list”, list) ; // request.getSession().setAttribute(“list”, list); return mapping.findForward(“listLibrarys”); } jsp中部分源代码: ${library.code} ${library.name} ${library.buildTime} “>添加 “>修改 “>删除 问题: 一次也不能循环. 在action的 List list = libraryManagerService.listLibrarys();后中加入: log.debug(“list size=”+list.size()); 运行结果:list size=6. 原因是: 中的redirect=”true”.直接转向后.request中的数据丢失. 改为redirect=”false”即可. redirect 决定了action在使用forward跳转的时候是使用 RequestDispatcher rd = getServletContext().getRequestDispatcher(uri).forward(request, response); 还是 response.sendRedirect(response.encodeRedirectURL(uri)); 在structs的类RequestProcessor中processForwardConfig方法里面有如下代码: if(forward.getRedirect())//判断redirect是否为true { if(uri.startsWith(“/“)) uri = request.getContextPath() + uri; response.sendRedirect(response.encodeRedirectURL(uri)); } else { doForward(uri, request, response); } 其中的方法doForward如下: protected void doForward(String uri, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { RequestDispatcher rd = getServletContext().getRequestDispatcher(uri); if(rd == null) { response.sendError(500, getInternal().getMessage(“requestDispatcher”, uri)); return; } else { rd.forward(request, response); return; } 这两种跳转方法的区别是: forward 会将 request state , bean 等等信息带往下一个 jsp redirect 是送到 client 端后,再由client向服务器发送request,服务器接收到后来发的request中已经不包含上次的request的state,bean等信息了。所以在jsp中就接收不到相应的数据

Linux系统之间的网络文件共享

A. 设置本地共享目录 a. 在 “/etc” 下新建名为 “exports” 的文件: “touch /etc/exports”。 b. 按如下格式添加要共享的目录及共享的访问权限: “/home host1(rw) host2(r) host3(w)” , 每个 目录一行。 c. 保存后重启系统或运行 “/sbin/service nfs restart”, 共享生效。 如果使用主机名(hostname)则必须将其添加到主机名列表中, 否则请使用IP 地址。 B. 挂载远程共享目录 a. 新建用来挂载的文件夹。 b. 使用命令 “mount r-host:/home/jelle/tmp /mnt/tmp” 或 “mount r-IP:/home/jelle/tmp /mnt/tmp” 手动 挂载远程主机 r-host/ 远程IP r-IP 上的共享目录; 或者在 “/etc/fstab” 中按如下格式添加自动挂载项: “r-host:/home /mnt/tmp nfs defaults” 或 “r-IP:/home /mnt/tmp nfs defaults”。

刷新父窗口怎样才能不弹出“重试”“取消”对话框

window.opener.location.reload();刷新网页弹出如下信息: 不重新发送信息,则无法刷新网页。 请单击“重试”再次发送信息,或单击“取消”返回正察看的页。 window.opener.location.reload(true); //true代表从服务器重新获取,false为从缓存中获取,默认为false window.operner.location.replace(window.opener.location); method=”post”或”get”造成的,你把method=”post”或”get”去掉就好了

SQL Server无法连接的问题

症状: 1、用java程序执行下列语句时,不能通过,也不出错,一直处于等待状态。 Connection conn = DriverManager.getConnection(url,”sa”,””); 2、用企业管理器不能打开数据库服务器。 3、查询分析器也不能打开数据库服务器。 原因:SQL Server协议启动顺序不正确。把tcp/ip放在最前即可。