android自动获取天气,android通过google api获取天气信息示例

android通过google API获取天气信息

public class WeatherActivity extends Activity {

private TextView txCity;

private Button btnSearch;

private Handler weatherhandler;

private Dialog progressDialog;

private Timer timer;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

timer = new Timer();

txCity = (TextView)findViewById(R.id.txCity);

btnSearch = (Button)findViewById(R.id.btnSearch);

progressDialog = new AlertDialog.Builder(this)

.setTitle("读取数据中")

.setMessage("正在加载数据,请稍等")

.create();

weatherhandler = new Handler(){

public void handleMessage(Message msg){

final String cityName = txCity.getText().toString().trim();

searchWeather(cityName);

progressDialog.hide();

}

};

btnSearch.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

progressDialog.show();

timer.schedule(new TimerTask() {

@Override

public void run() {

Message msg = new Message();

msg.setTarget(weatherhandler);

msg.sendToTarget();

}

},100);

}

});

}

private void searchWeather(String city){

SAXParserFactory spf = SAXParserFactory.newInstance();

try {

SAXParser sp = spf.newSAXParser();

XMLReader reader = sp.getXMLReader();

XmlHandler handler = new XmlHandler();

reader.setContentHandler(handler);

URL url = new URL("http://www.google.com/ig/api?hl=zh-cn&weather="+URLEncoder.encode(city));

InputStream is = url.openStream();

InputStreamReader isr = new InputStreamReader(is, "GBK");

InputSource source = new InputSource(isr);

reader.parse(source);

ListweatherList = handler.getWeatherList();

TableLayout table = (TableLayout)findViewById(R.id.table);

table.removeAllViews();

for(Weather weather:weatherList){

TableRow row = new TableRow(this);

row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

row.setGravity(Gravity.CENTER_VERTICAL);

ImageView img = new ImageView(this);

img.setImageDrawable(loadImage(weather.getImageUrl()));

img.setMinimumHeight(80);

row.addView(img);

TextView day = new TextView(this);

day.setText(weather.getDay());

day.setGravity(Gravity.CENTER_HORIZONTAL);

row.addView(day);

TextView temp = new TextView(this);

temp.setText(weather.getLowTemp()+"℃-"+weather.getHighTemp()+"℃");

temp.setGravity(Gravity.CENTER_HORIZONTAL);

row.addView(temp);

TextView condition = new TextView(this);

condition.setText(weather.getCondition());

condition.setGravity(Gravity.CENTER_HORIZONTAL);

row.addView(condition);

table.addView(row);

}

} catch (Exception e) {

e.printStackTrace();

new AlertDialog.Builder(this)

.setTitle("解析错误")

.setMessage("获取天气数据失败,请稍候再试。")

.setNegativeButton("确定", null)

.show();

}

}

private Drawable loadImage(String imageUrl) {

try {

return Drawable.createFromStream((InputStream) new URL("http://www.google.com/"+imageUrl).getContent(), "test");

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

}

<think>好的,我现在要解决用户在IAR编译器中遇到的错误:“incomplete type is not allowed”。首先,我需要理解这个错误的含义和常见原因。根据我的知识库,这个错误常发生在代码中使用了未完全定义的类型,比如结构体或联合体声明了但未定义,或者前向声明了某个类型但没有提供完整的定义。 首先,我应该检查用户代码中是否有结构体或联合体的声明但没有定义的情况。比如,如果有一个结构体声明为`struct MyStruct;`,但没有后续的完整定义,那么在需要该结构体大小或成员的地方,编译器就会报这个错误。这时候,需要用户补全结构体的定义,包括成员变量。 其次,检查是否在头文件中只声明了类型而没有包含定义。例如,如果在头文件中使用前向声明,但在使用该类型的源文件中没有包含实际的定义头文件,这也会导致错误。此时,需要确保在使用类型的地方包含了正确的头文件,或者将前向声明替换为完整的定义。 另外,还要考虑typedef的问题。如果用户使用了typedef创建别名,但原始类型未完全定义,同样会引发错误。例如,`typedef struct MyStruct MyType;`之后,如果尝试使用MyType但MyStruct尚未定义,就会出错。需要确保在使用typedef之前,原始类型已经被完整定义。 此外,数组声明中的类型不完整也是一个常见原因。比如声明数组时,如果元素类型未完全定义,编译器无法确定数组的大小,导致错误。需要检查数组声明中的类型是否已经完整定义。 可能还有其他情况,比如在C和C++混合编程时,如果没有正确使用extern "C",或者在头文件中遗漏了必要的包含,也可能导致类型不完整。需要用户检查这些方面。 接下来,我需要将这些可能的原因整理成清晰的解决步骤,并给出具体的代码示例,帮助用户理解如何修改。同时,根据用户提供的引用内容,虽然引用[1]和引用[2]是关于其他错误的,但用户可能希望看到类似的结构,即分点说明解决方案,并引用相关资源。 最后,生成相关问题,帮助用户进一步了解可能相关的编译错误,比如类型未定义、前向声明问题等。这样用户可以有更全面的认识,预防其他类似错误的发生。</think>### IAR编译器错误'incomplete type is not allowed'解决方案 该错误表示代码中使用了**未完全定义的类型**,编译器无法确定其内存布局或成员信息。以下是具体原因及解决方法: #### 1. 结构体/联合体未定义完整 若声明了结构体但未定义成员,在实例化时会报错: ```c struct MyStruct; // 前向声明不完整 struct MyStruct var; // 错误:使用未完成类型 ``` **解决方案**:补全类型定义 ```c struct MyStruct { int id; char name[20]; }; ``` #### 2. 头文件包含缺失 当跨文件使用类型时,需确保定义可见: ```c // file.h struct Data; // 前向声明 // file.c struct Data { // 实际定义 int value; }; ``` **解决方案**:在使用该类型的文件中包含定义头文件 ```c #include "file.c" // 包含实际定义 ``` #### 3. typedef别名问题 使用typedef时原始类型必须完整: ```c typedef struct Node NodeT; // 前向声明 NodeT* ptr; // 允许指针声明 NodeT instance; // 错误:不完整类型 ``` **解决方案**:先完成类型定义再typedef ```c struct Node { int data; struct Node* next; }; typedef struct Node NodeT; ``` #### 4. 数组声明不完整 数组元素类型必须完全定义: ```c struct Element; struct Element arr[10]; // 错误:元素类型未定义 ``` **解决方案**: ```c struct Element { int type; float value; }; struct Element arr[10]; // 合法 ``` #### 调试建议 1. 在IAR工程中搜索错误行号定位问题代码 2. 使用Go to Definition功能追踪类型定义 3. 检查所有头文件包含链 4. 确认没有循环依赖的头文件 编译器需要知道类型的完整信息才能: - 计算sizeof大小 - 分配内存空间 - 访问成员变量 - 进行类型对齐 [^1]: 类似类型转换错误可参考浮点转整型的类型适配问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值