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

开发者社区> 问答> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

android 天气预报问题?报错

请问为什么运行的时候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

展开
收起
爱吃鱼的程序员 2020-06-22 19:59:45 303 0
1 条回答
写回答
取消 提交回答
  • 爱吃鱼的程序员
    https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

     你把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-:运行时异常哈!报错是不能实例化!请问应该怎么改正(┳_┳)...单步调试一下看看。自己调试一下就知道了,拿出这么长的代码问。。。
    2020-06-22 20:00:02
    赞同 展开评论 打赏
问答分类:
相关产品:
问答排行榜
最热
最新
相关电子书
更多
Android组件化实现
立即下载
蚂蚁聚宝Android秒级编译—— Freeline
立即下载
Android插件化:从入门到放弃
立即下载