android 天气预报问题?报错-问答-阿里云开发者社区-阿里云

开发者社区> 爱吃鱼的程序员> 正文

android 天气预报问题?报错

2020-06-22 19:59:45 97 1

请问为什么运行的时候log cat报错?

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.Weibo;
import com.weibo.sdk.android.WeiboAuthListener;
import com.weibo.sdk.android.WeiboDialogError;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.StatusesAPI;
import com.weibo.sdk.android.net.RequestListener;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class WeatherActivity extends Activity {
	
	private static final String CONSUMER_KEY = "1646212860";// 替换为开发者的appkey,例如"1646212860";
	private static final String REDIRECT_URL = "http://www.sina.com";
	static Oauth2AccessToken accessToken;
	
	Weibo mWeibo;

	EditText et = (EditText) findViewById(R.id.editText1);
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);// ȥ�����8
		setContentView(R.layout.main);
		
		Button b = (Button) findViewById(R.id.button1);
		Button b2 = (Button) findViewById(R.id.button2);
		Button b3 = (Button) findViewById(R.id.button3);
		
		
		mWeibo = Weibo.getInstance(CONSUMER_KEY, REDIRECT_URL);
		
		b.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				String cityname = et.getText().toString();
				getWeather(cityname);
				
			}
		});
		
		b2.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mWeibo.authorize(WeatherActivity.this, new AuthDialogListener());
			}});
		
		
		b3.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String cityname = et.getText().toString();
				share(cityname);
			}
			
		});
		
	}
	
	protected void getWeather(String city){
		String WSDL_URI = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";//wsdl ��uri
		String namespace = "http://WebXml.com.cn/";//namespace
		String methodName = "getWeather";//Ҫ���õķ������
		
		SoapObject request = new SoapObject(namespace, methodName);
		request.addProperty("theCityCode", city);
		try{
			//����SoapSerializationEnvelope ����ͬʱָ��soap�汾��(֮ǰ��wsdl�п�����)
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER12);
			envelope.bodyOut = request;//�����Ƿ�����������������bodyOut
			envelope.dotNet = true;//������.net������webservice����������Ҫ����Ϊtrue
			
			HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL_URI);
			httpTransportSE.call(null, envelope);//����
			
			SoapObject response = (SoapObject) envelope.getResponse();//��÷��ض���
			if(response!=null){
				
				TextView t1 = (TextView) findViewById(R.id.textView1);
				t1.setText(response.getProperty(1).toString());
				
				
				TextView t2 = (TextView) findViewById(R.id.textView2);
				t2.setText(response.getProperty(3).toString());
				
				
				TextView t3 = (TextView) findViewById(R.id.textView3);
				t3.setText(response.getProperty(4).toString());
				
				
				TextView t4 = (TextView) findViewById(R.id.textView4);
				t4.setText(response.getProperty(5).toString());
				
				
				ImageView im1 = (ImageView) findViewById(R.id.imageView1);
				int icon1 =parseIcon(response.getProperty(10).toString());
				im1.setImageResource(icon1);
				
				
				ImageView im2 = (ImageView) findViewById(R.id.imageView2);
				int icon2 =parseIcon(response.getProperty(11).toString());
				im2.setImageResource(icon2);
				
			}
			
		}catch(Exception e){
			e.printStackTrace();
			}
		
	}
	
	protected void share(String city){
		String WSDL_URI = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";//wsdl ��uri
		String namespace = "http://WebXml.com.cn/";//namespace
		String methodName = "getWeather";//Ҫ���õķ������
		
		SoapObject request = new SoapObject(namespace, methodName);
		request.addProperty("theCityCode", city);
		try{
			//����SoapSerializationEnvelope ����ͬʱָ��soap�汾��(֮ǰ��wsdl�п�����)
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER12);
			envelope.bodyOut = request;//�����Ƿ�����������������bodyOut
			envelope.dotNet = true;//������.net������webservice����������Ҫ����Ϊtrue
			
			HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL_URI);
			httpTransportSE.call(null, envelope);//����
			
			SoapObject response = (SoapObject) envelope.getResponse();//��÷��ض���
			if(response!=null){
				StatusesAPI sa = new StatusesAPI(accessToken);
				String content = response.getProperty(1).toString() + "  " + response.getProperty(3).toString() + "  " + response.getProperty(4).toString();
				sa.update(content, "0", "0", listener);
			}
			}catch(Exception e){
				e.printStackTrace();
				}
	}
	
	// ���߷������÷�������ѷ��ص�����ͼ���ַ�ת��Ϊ�����ͼƬ��ԴID��
		private int parseIcon(String strIcon)
		{
			if (strIcon == null)
				return -1;
			if ("0.gif".equals(strIcon))
				return R.drawable.a_0;
			if ("1.gif".equals(strIcon))
				return R.drawable.a_1;
			if ("2.gif".equals(strIcon))
				return R.drawable.a_2;
			if ("3.gif".equals(strIcon))
				return R.drawable.a_3;
			if ("4.gif".equals(strIcon))
				return R.drawable.a_4;
			if ("5.gif".equals(strIcon))
				return R.drawable.a_5;
			if ("6.gif".equals(strIcon))
				return R.drawable.a_6;
			if ("7.gif".equals(strIcon))
				return R.drawable.a_7;
			if ("8.gif".equals(strIcon))
				return R.drawable.a_8;
			if ("9.gif".equals(strIcon))
				return R.drawable.a_9;
			if ("10.gif".equals(strIcon))
				return R.drawable.a_10;
			if ("11.gif".equals(strIcon))
				return R.drawable.a_11;
			if ("12.gif".equals(strIcon))
				return R.drawable.a_12;
			if ("13.gif".equals(strIcon))
				return R.drawable.a_13;
			if ("14.gif".equals(strIcon))
				return R.drawable.a_14;
			if ("15.gif".equals(strIcon))
				return R.drawable.a_15;
			if ("16.gif".equals(strIcon))
				return R.drawable.a_16;
			if ("17.gif".equals(strIcon))
				return R.drawable.a_17;
			if ("18.gif".equals(strIcon))
				return R.drawable.a_18;
			if ("19.gif".equals(strIcon))
				return R.drawable.a_19;
			if ("20.gif".equals(strIcon))
				return R.drawable.a_20;
			if ("21.gif".equals(strIcon))
				return R.drawable.a_21;
			if ("22.gif".equals(strIcon))
				return R.drawable.a_22;
			if ("23.gif".equals(strIcon))
				return R.drawable.a_23;
			if ("24.gif".equals(strIcon))
				return R.drawable.a_24;
			if ("25.gif".equals(strIcon))
				return R.drawable.a_25;
			if ("26.gif".equals(strIcon))
				return R.drawable.a_26;
			if ("27.gif".equals(strIcon))
				return R.drawable.a_27;
			if ("28.gif".equals(strIcon))
				return R.drawable.a_28;
			if ("29.gif".equals(strIcon))
				return R.drawable.a_29;
			if ("30.gif".equals(strIcon))
				return R.drawable.a_30;
			if ("31.gif".equals(strIcon))
				return R.drawable.a_31;
			return 0;
		}
		
		class AuthDialogListener implements WeiboAuthListener{

			@Override
			public void onCancel() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onComplete(Bundle arg0) {
				// TODO Auto-generated method stub
				String token = arg0.getString("access_token");
	            String expires_in = arg0.getString("expires_in");
	            WeatherActivity.accessToken = new Oauth2AccessToken(token, expires_in);
				
			}

			@Override
			public void onError(WeiboDialogError arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onWeiboException(WeiboException arg0) {
				// TODO Auto-generated method stub
				
			}}
		
		RequestListener listener = new RequestListener(){

			@Override
			public void onComplete(String arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onError(WeiboException arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onIOException(IOException arg0) {
				// TODO Auto-generated method stub
				
			}};
}

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.weather2/com.example.weather2.WeatherActivity}: java.lang.NullPointerException

取消 提交回答
全部回答(1)
  • 爱吃鱼的程序员
    2020-06-22 20:00:02

     你把EditTextet=(EditText)findViewById(R.id.editText1);

    放在setContentView(R.layout.main);后面试试吧。

    看下面的代码:

    publicViewfindViewById(intid){

           returngetWindow().findViewById(id);

       }

     publicWindowgetWindow(){
           returnmWindow;
       }


    谢谢,已经解决了,确实是个问题O(∩_∩)O~~


    不知道什么原因,

    不过EditTextet=(EditText)findViewById(R.id.editText1);

    这句放在onCreate外什么意思

    去年毕设我同学就是用这个程序的....o(︶︿︶)o唉

    请问是什么问题吗?从图中weather2.weatherActivity空指针回复 @-Mop-:运行时异常哈!报错是不能实例化!请问应该怎么改正(┳_┳)...单步调试一下看看。自己调试一下就知道了,拿出这么长的代码问。。。
    0 0
相关问答

40

回答

[@徐雷frank][¥20]什么是JAVA的平台无关性

大河人家 2018-10-29 23:55:20 144259浏览量 回答数 40

17

回答

【大咖问答】对话PostgreSQL 中国社区发起人之一,阿里云数据库高级专家 德哥

阿里ACE 彭飞 2019-07-10 09:36:10 966254浏览量 回答数 17

162

回答

惊喜翻倍:免费ECS+免费环境配置~!(ECS免费体验6个月活动3月31日结束)

豆妹 2014-10-29 17:52:21 222979浏览量 回答数 162

8

回答

OceanBase 使用动画(持续更新)

mq4096 2019-02-20 17:16:36 333855浏览量 回答数 8

110

回答

OSS存储服务-客户端工具

newegg11 2012-05-17 15:37:18 292270浏览量 回答数 110

22

回答

爬虫数据管理【问答合集】

我是管理员 2018-08-10 16:37:41 146769浏览量 回答数 22

18

回答

阿里云开放端口权限

xcxx 2016-07-20 15:03:33 643103浏览量 回答数 18

31

回答

[@倚贤][¥20]刚学完html/css/js的新手学习servlet、jsp需要注意哪些问题?

弗洛伊德6 2018-10-27 21:52:43 145607浏览量 回答数 31

1

回答

阿里云开发者论坛一周精粹期刊汇总 -  2018.12

福利达人 2018-03-06 16:42:35 205819浏览量 回答数 1

24

回答

【精品问答】python技术1000问(1)

问问小秘 2019-11-15 13:25:00 471375浏览量 回答数 24
+关注
爱吃鱼的程序员
https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB
2
文章
21569
问答
推荐问答
相关电子书
更多
文娱运维技术
立即下载
《SaaS模式云原生数据仓库应用场景实践》
立即下载
《看见新力量:二》电子书
立即下载