开始学习android的开发,最开始想想,应该和MFC差不多。他们都是用现成的框架,用系统API,然后是面向对象,这样想倒是觉得蛮简单,所以信誓旦旦的答应老师自己可以做的很好,很快上手。还有就是以前看过一篇文章,说的是做php的直接因为公司需要,开始做android,从零开始,到产品发布,所花费的时间是一个月!我觉得嘛,他可以我也可以!然后就匆匆上路,去图书馆借了两本书,自己买了一本!
第一周,什么都没做,到周三才去老师那里,搭配环境,了解了几个基本控件,而且他让我先看关于解析XML文件的东西。因为以前自己在windows和ubuntu上面都有过搭配的经历,所以一个下午就搞定了。后面就是各种看书,学习怎么用java(其实是套着C#的思路),当时就遇到了一点麻烦,什么extends、super、implements,看看继承多态封装以及package方面的东西,因为以为是一样的,没怎么看,后来还真是挺麻烦的啊!不过就这么磕磕绊绊的快走过第一周,直到周五下午,谭哥给我布置了个任务,就是下个程序,下载网络图片,然后把它显示出来。在这个过程,实现不是很顺利,周五一个下午没有做出来,虽然看了很多网上的例子,但是我写了却实现不了,又因为周六要全天去工作室做事,周日白天也是,只剩一个周日下午了!心里还是想是有点压力的啊,做不好多尴尬嘛!
现在从周五开始说说细节。
周五的晚上,一直在工作室调android,因为工作室的任务完成了。从网上找了几个实例,但是在本地实现不了,各种错误,然后自己每个都看了,归纳了下大致过程,然后自己试着去完成,一直没有成功。刚写完的时候,一直纠结调试不方便,然后想着弹个AlertWindow,让他报告程序中各变量的值,但是因为是在后台程序,下载tools包里面的,结果不成功,找了很多原因,没解决!后来补了下android 上debug 的方式,又借来个android手机,慢慢就学了点!
到了周六晚上,从晚上十一点开始调,高了半天Toast函数,放在其他包里没用,只好放在主Activity里面让它实现,然后写了个tools函数,用来从指定的URL里面下载图片,各种不成功,但是没用认真看错误信息(瓜的吧,嘻嘻),然后就各种不知道问题!后面折腾了半天,慢慢开始看错误信息,终于发现没用给网络访问的权限,然后加上了。但是接着调,还是不行,结果是网络访问不了,很纠结啊!然后到凌晨两点,受不了了,睡觉。
周日哦,在工作室,本来6点左右下班,但是因为今天天气不错,大家吵着去打球,所以去耍了,直到下午六点半,回寝室准备去洗澡!脱了衣服,在电脑前犹豫了一下,接着开始调程序,然后就仔细看错误信息。发现错误里面说Fatal error,Main,然后bing了一下,就发现时xml文件配置有问题,然后就改了,后来还是有好多问题,觉得纠结,就把tools里面的代码放到Activity函数里面,然后一调就ok饿。安心去洗澡了。最后的程序啊是这样的
package pictureyou.main;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.Button;
public class PictureYouActivity extends Activity {
private ImageView imgView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imgView = (ImageView)findViewById(R.id.imageView1);
Toast.makeText(getBaseContext(), "好像可以用", Toast.LENGTH_SHORT).show();
try
{
URL url = new URL("http://bbs.qshpan.com/images/newind/logo.gif");
try
{
Bitmap bit = null;
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//Bitmap bit = getImage(url);
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bit = BitmapFactory.decodeStream(is);
if(bit != null)
{
imgView.setImageBitmap(bit);
}
else
{
Bitmap bitt1 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
imgView.setImageBitmap(bitt1);
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
catch(MalformedURLException err)
{
err.printStackTrace();
}
Button bu = (Button)findViewById(R.id.button1);
bu.setOnClickListener(new Button.OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
Bitmap bitt1 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
imgView.setImageBitmap(bitt1);
}
});
}
public Bitmap getImage(URL url) throws IOException
{
Bitmap bit = null;
try{
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
int ints = conn.getResponseCode();
if(ints == HttpURLConnection.HTTP_OK){
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
int length = (int)conn.getContentLength();
if(length != -1){
// byte[] imageContent = new byte[length];
// byte[] buff = new byte[512];
// int readLength = 0,desPos = 0;
// while((readLength = is.read(buff))>0){
// System.arraycopy(buff,0,imageContent,desPos,readLength);
bit = BitmapFactory.decodeStream(is);
}
//bit = BitmapFactory.decodeByteArray(imageContent, 0, imageContent.length);
}
else
{
bit = BitmapFactory.decodeResource(getResources(), R.drawable.a123);
}
}
catch(IOException e)
{
e.printStackTrace();
}
return bit;
}
}
xml文件这样
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.24"
android:text="Button" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/vv1"
android:layout_weight="0.24"
android:src="@drawable/a123" />
其中tools里面是这样,虽然没有用,可以参考我移动的代码
package pictureyou.tools;
import pictureyou.main.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.R;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import android.view.LayoutInflater;
import pictureyou.main.*;
public class LoadRemoteImage {
public static Bitmap getImage(URL url,Object obj) throws IOException
{
Bitmap bit = null;
try{
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
int length = (int)conn.getContentLength();
if(length != -1){
byte[] imageContent = new byte[length];
byte[] buff = new byte[512];
int readLength = 0,desPos = 0;
while((readLength = is.read(buff))>0){
System.arraycopy(buff,0,imageContent,desPos,readLength);
}
bit = BitmapFactory.decodeByteArray(imageContent, 0, imageContent.length);
}
}
catch(IOException e)
{
e.printStackTrace();
}
return bit;
}
}
androidmanifest.xml里面的权限添加
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pictureyou.main"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="9" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".PictureYouActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
总结下嘛,就是以后认真看错误日志,没有错误日志,靠自己臆测,对于刚接触的方面还是不靠谱啊!
学从难处学,用从易处用。不要一开始把问题搞得那么复杂,实现了基本的需求才是根本。这个我在面向对象的编程实践里面看到过,虽然原话不是这么说的,但差不多这个意思(学习不够深入),以后得注意了!比如什么AlertWindows,Toast都是无关紧要的东西,不该弄啊。
还有吧,这种速成的东西,看例子很重要啊!学习速度可以很快的!
转载于:https://www.cnblogs.com/imhurley/archive/2012/03/25/2417243.html