Android 天气预报 - 错误
我已经使用此链接进行了天气预报
http://www.anddev.org/android_weather_forecast_ -_google_weather_api__-_full_source-t361.html。
我收到以下错误:
08-09 10:57:45.737: ERROR/AndroidRuntime(539): FATAL EXCEPTION: main
08-09 10:57:45.737: ERROR/AndroidRuntime(539): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.anddev.android.weatherforecast/org.anddev.android.weatherforecast.WeatherForecast}: android.view.InflateException: Binary XML file line #55: Error inflating class org.anddev.android.weatherforecast.views.SingleWeatherInfoView
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.os.Handler.dispatchMessage(Handler.java:99)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.os.Looper.loop(Looper.java:123)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at java.lang.reflect.Method.invokeNative(Native Method)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at java.lang.reflect.Method.invoke(Method.java:507)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at dalvik.system.NativeStart.main(Native Method)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): Caused by: android.view.InflateException: Binary XML file line #55: Error inflating class org.anddev.android.weatherforecast.views.SingleWeatherInfoView
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.view.LayoutInflater.createView(LayoutInflater.java:508)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.app.Activity.setContentView(Activity.java:1657)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at org.anddev.android.weatherforecast.WeatherForecast.onCreate(WeatherForecast.java:36)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): ... 11 more
08-09 10:57:45.737: ERROR/AndroidRuntime(539): Caused by: java.lang.NoSuchMethodException: SingleWeatherInfoView(Context,AttributeSet)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at java.lang.Class.getMatchingConstructor(Class.java:643)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at java.lang.Class.getConstructor(Class.java:472)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.view.LayoutInflater.createView(LayoutInflater.java:480)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): ... 21 more
这是我的 Xml 文件 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/weather_background"
>
<TableLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Usage like: 'City, Country'"
android:gravity="left"
/>
<CheckBox android:id="@+id/chk_usecelsius"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Use °C (not °F)"
android:gravity="right"
/>
</TableRow>
</TableLayout>
<TableLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText android:id="@+id/edit_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Schriesheim, Germany"
android:singleLine="true"
/>
<Button android:id="@+id/cmd_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
/>
</TableRow>
</TableLayout>
<org.anddev.android.weatherforecast.views.SingleWeatherInfoView
android:id="@+id/weather_today"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="15px"
/>
<TableLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1,2,3"
android:paddingTop="15px"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<org.anddev.android.weatherforecast.views.SingleWeatherInfoView
android:id="@+id/weather_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
/>
<org.anddev.android.weatherforecast.views.SingleWeatherInfoView
android:id="@+id/weather_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
/>
<org.anddev.android.weatherforecast.views.SingleWeatherInfoView
android:id="@+id/weather_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
/>
<org.anddev.android.weatherforecast.views.SingleWeatherInfoView
android:id="@+id/weather_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
/>
</TableRow>
</TableLayout>
,这是我的类文件
public class SingleWeatherInfoView extends LinearLayout {
private ImageView myWeatherImageView = null;
private TextView myTempTextView = null;
public SingleWeatherInfoView(Context context) {
super(context);
}
public SingleWeatherInfoView(Context context, AttributeSet attrs,
Map inflateParams) {
super(context, attrs);
/* Setup the ImageView that will show weather-icon. */
this.myWeatherImageView = new ImageView(context);
this.myWeatherImageView.setImageDrawable(getResources().getDrawable(
R.drawable.dunno));
/* Setup the textView that will show the temperature. */
this.myTempTextView = new TextView(context);
this.myTempTextView.setText("? °C");
this.myTempTextView.setTextSize(16);
this.myTempTextView.setTypeface(Typeface
.create("Tahoma", Typeface.BOLD));
/* Add child views to this object. */
this.addView(this.myWeatherImageView, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
this.addView(this.myTempTextView, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
public void reset() {
this.myWeatherImageView.setImageDrawable(getResources().getDrawable(
R.drawable.dunno));
this.myTempTextView.setText("? °C");
}
/** Sets the Child-ImageView of this to the URL passed. */
public void setRemoteImage(URL aURL) {
try {
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
this.myWeatherImageView.setImageBitmap(bm);
} catch (IOException e) {
/* Reset to 'Dunno' on any error. */
this.myWeatherImageView.setImageDrawable(getResources()
.getDrawable(R.drawable.dunno));
}
}
public void setTempCelcius(int aTemp) {
this.myTempTextView.setText("" + aTemp + " °C");
}
public void setTempFahrenheit(int aTemp) {
this.myTempTextView.setText("" + aTemp + " °F");
}
public void setTempFahrenheitMinMax(int aMinTemp, int aMaxTemp) {
this.myTempTextView.setText("" + aMinTemp + "/" + aMaxTemp + " °F");
}
public void setTempCelciusMinMax(int aMinTemp, int aMaxTemp) {
this.myTempTextView.setText("" + aMinTemp + "/" + aMaxTemp + " °C");
}
public void setTempString(String aTempString) {
this.myTempTextView.setText(aTempString);
}
}
导致这些错误的原因是什么?
I have done weather forecast using this link
http://www.anddev.org/android_weather_forecast_-_google_weather_api_-_full_source-t361.html.
I got following error :
08-09 10:57:45.737: ERROR/AndroidRuntime(539): FATAL EXCEPTION: main
08-09 10:57:45.737: ERROR/AndroidRuntime(539): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.anddev.android.weatherforecast/org.anddev.android.weatherforecast.WeatherForecast}: android.view.InflateException: Binary XML file line #55: Error inflating class org.anddev.android.weatherforecast.views.SingleWeatherInfoView
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.os.Handler.dispatchMessage(Handler.java:99)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.os.Looper.loop(Looper.java:123)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at java.lang.reflect.Method.invokeNative(Native Method)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at java.lang.reflect.Method.invoke(Method.java:507)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at dalvik.system.NativeStart.main(Native Method)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): Caused by: android.view.InflateException: Binary XML file line #55: Error inflating class org.anddev.android.weatherforecast.views.SingleWeatherInfoView
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.view.LayoutInflater.createView(LayoutInflater.java:508)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.app.Activity.setContentView(Activity.java:1657)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at org.anddev.android.weatherforecast.WeatherForecast.onCreate(WeatherForecast.java:36)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): ... 11 more
08-09 10:57:45.737: ERROR/AndroidRuntime(539): Caused by: java.lang.NoSuchMethodException: SingleWeatherInfoView(Context,AttributeSet)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at java.lang.Class.getMatchingConstructor(Class.java:643)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at java.lang.Class.getConstructor(Class.java:472)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): at android.view.LayoutInflater.createView(LayoutInflater.java:480)
08-09 10:57:45.737: ERROR/AndroidRuntime(539): ... 21 more
This is my Xml File main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/weather_background"
>
<TableLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Usage like: 'City, Country'"
android:gravity="left"
/>
<CheckBox android:id="@+id/chk_usecelsius"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Use °C (not °F)"
android:gravity="right"
/>
</TableRow>
</TableLayout>
<TableLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText android:id="@+id/edit_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Schriesheim, Germany"
android:singleLine="true"
/>
<Button android:id="@+id/cmd_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
/>
</TableRow>
</TableLayout>
<org.anddev.android.weatherforecast.views.SingleWeatherInfoView
android:id="@+id/weather_today"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="15px"
/>
<TableLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1,2,3"
android:paddingTop="15px"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<org.anddev.android.weatherforecast.views.SingleWeatherInfoView
android:id="@+id/weather_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
/>
<org.anddev.android.weatherforecast.views.SingleWeatherInfoView
android:id="@+id/weather_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
/>
<org.anddev.android.weatherforecast.views.SingleWeatherInfoView
android:id="@+id/weather_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
/>
<org.anddev.android.weatherforecast.views.SingleWeatherInfoView
android:id="@+id/weather_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
/>
</TableRow>
</TableLayout>
and this is my class file
public class SingleWeatherInfoView extends LinearLayout {
private ImageView myWeatherImageView = null;
private TextView myTempTextView = null;
public SingleWeatherInfoView(Context context) {
super(context);
}
public SingleWeatherInfoView(Context context, AttributeSet attrs,
Map inflateParams) {
super(context, attrs);
/* Setup the ImageView that will show weather-icon. */
this.myWeatherImageView = new ImageView(context);
this.myWeatherImageView.setImageDrawable(getResources().getDrawable(
R.drawable.dunno));
/* Setup the textView that will show the temperature. */
this.myTempTextView = new TextView(context);
this.myTempTextView.setText("? °C");
this.myTempTextView.setTextSize(16);
this.myTempTextView.setTypeface(Typeface
.create("Tahoma", Typeface.BOLD));
/* Add child views to this object. */
this.addView(this.myWeatherImageView, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
this.addView(this.myTempTextView, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
public void reset() {
this.myWeatherImageView.setImageDrawable(getResources().getDrawable(
R.drawable.dunno));
this.myTempTextView.setText("? °C");
}
/** Sets the Child-ImageView of this to the URL passed. */
public void setRemoteImage(URL aURL) {
try {
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
this.myWeatherImageView.setImageBitmap(bm);
} catch (IOException e) {
/* Reset to 'Dunno' on any error. */
this.myWeatherImageView.setImageDrawable(getResources()
.getDrawable(R.drawable.dunno));
}
}
public void setTempCelcius(int aTemp) {
this.myTempTextView.setText("" + aTemp + " °C");
}
public void setTempFahrenheit(int aTemp) {
this.myTempTextView.setText("" + aTemp + " °F");
}
public void setTempFahrenheitMinMax(int aMinTemp, int aMaxTemp) {
this.myTempTextView.setText("" + aMinTemp + "/" + aMaxTemp + " °F");
}
public void setTempCelciusMinMax(int aMinTemp, int aMaxTemp) {
this.myTempTextView.setText("" + aMinTemp + "/" + aMaxTemp + " °C");
}
public void setTempString(String aTempString) {
this.myTempTextView.setText(aTempString);
}
}
What causes these errors?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需将“Setup ImageView...”之后的所有逻辑移动到一个新方法 init(...) 中,并从移动逻辑的位置调用它。
创建
公共 SingleWeatherInfoView(上下文上下文,AttributeSet attrs){
超级(上下文,属性);
初始化(...);
}
Just move all the logic after Setup ImageView... into a new method init(...) and call it from where you moved the logic.
Create
public SingleWeatherInfoView(Context context, AttributeSet attrs) {
super(context, attrs);
init(...);
}
您正在使用的 SingleWeatherInfoView 类似乎无法正确支持 XML 膨胀。您应该尝试从代码而不是 XML 创建它的实例,看看是否有效。
It looks like the SingleWeatherInfoView class you are using doesn't properly support XML inflation. You should try creating instances of it from code instead of XML and see if that works.
我遇到了同样的问题,我犯的错误是在 main.xml 中为包 SingleWeatherInfoView 添加了错误的地址:
请确保您的项目具有此路径。
I had the same problem and the error I was making was putting an incorrect address for the package SingleWeatherInfoView in main.xml:
Be sure that your project has this path.