学习android的新手,在做例子中不断前行,感谢大家给我帮助,这里遇到了一个问题,恳请各位大哥大姐指教!

lostmoon 2014-07-05 11:22:14
做的是一个验证用户登录的例子。用的是webservice,这个webservice代码是没有问题的,就是一个UserLogin方法,参数是用户名和密码,调用方法后返回true或false,表示用户名和密码是否都正确。android代码,我改编自手机归属地验证的程序,这个程序本身也是正确的,我已经调试过,可以运行,我照葫芦画瓢,改成验证用户的程序,就有错误提示了,实在看不出在什么地方了。

一个错误是出在“getRemoteInfo(UserName,Pwd);” ,提示的错误信息是“Cannot make a static reference to the non-static method getRemoteInfo(String, String) from the type MainActivity”。

一个错误是出在“TextViewResult.setText(msg.obj.toString());”,提示的错误信息是“TextViewResult cannot be resolved”。

java源代码如下:




package com.example.click;

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

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

private EditText UserNameEditText;
private EditText PassEditText;
private Button queryButton;
private TextView TextViewResult;


public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);




UserNameEditText = (EditText) rootView.findViewById(R.id.name);
PassEditText = (EditText) rootView.findViewById(R.id.pass);
queryButton = (Button) rootView.findViewById(R.id.button);
TextViewResult = (TextView) rootView.findViewById(R.id.result_text);

queryButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v)
{


String UserName = UserNameEditText.getText().toString().trim();
String Pwd = PassEditText.getText().toString().trim();
TextViewResult.setText("用户名" + UserName + "," + "密码" + Pwd);

Thread th= new Thread(new Runnable() {

@Override
public void run() {

getRemoteInfo(UserName,Pwd);

}
});

th.start();



/*Intent intent = new Intent(getActivity(), OtherActivity.class); //this 即当前 Activity
startActivity(intent);
*/

}
});



return rootView;
}
}


/**
* 手机号段归属地查询
*
* @param phoneSec 手机号段
*/
public void getRemoteInfo(String username,String password) {
// 命名空间
String nameSpace = "http://tempuri.org/";
// 调用的方法名称
String methodName = "UserLogin";
// EndPoint
String endPoint = "http://10.0.2.2/webservice1.asmx";
// SOAP Action
String soapAction = "http://tempuri.org/UserLogin";

// 指定WebService的命名空间和调用的方法名
SoapObject rpc = new SoapObject(nameSpace, methodName);

// 设置需调用WebService接口需要传入的两个参数mobileCode、userId
rpc.addProperty("username", username);
rpc.addProperty("password", password);

// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.bodyOut = rpc;
// 设置是否调用的是dotNet开发的WebService
envelope.dotNet = true;
// 等价于envelope.bodyOut = rpc;
envelope.setOutputSoapObject(rpc);

HttpTransportSE transport = new HttpTransportSE(endPoint);
try {
// 调用WebService
transport.call(soapAction, envelope);
} catch (Exception e) {
e.printStackTrace();
}

// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
// 获取返回的结果
String result = object.getProperty(0).toString();

Message msg = Message.obtain();
msg.obj = result;
mHandler.sendMessage(msg);


// 将WebService返回的结果显示在TextView中
// resultView.setText(result);
}

Handler mHandler = new Handler(){
public void handleMessage(Message msg) {
TextViewResult.setText(msg.obj.toString());
};
};




}





肯定各位android长辈,帮我看看,问题出在什么地方了,怎么解决?谢谢啦!
我的开发平台是android4.4。
...全文
170 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
张大熊 2014-07-06
  • 打赏
  • 举报
回复
public void getRemoteInfo(String username,String password){.....} 改成 public static void getRemoteInfo(String username,String password){.....} TextViewResult.setText(msg.obj.toString()) 改成TextViewResult.setText((String)msg.obj); 感觉你不需要把那个类定义成静态的,不知道你的代码是哪里来的
奋斗中的显摆 2014-07-06
  • 打赏
  • 举报
回复
public static class PlaceholderFragment extends Fragment 你为什么要把它定义成static的? 没有理解;去掉static 应该就没问题了;static 的话说明这个类 是不属于MainActivity,你不能直接调用MainActivity的方法
風言楓語 2014-07-06
  • 打赏
  • 举报
回复
Cannot make a static reference to the non-static method getRemoteInfo(String, String) from the type MainActivity 不能做一个静态引用非静态方法getRemoteInfo MainActivity类型(字符串,字符串) 你在静态方法里面调用了非静态方法
lostmoon 2014-07-05
  • 打赏
  • 举报
回复
不能。错误就是我描述中写的
sagittarius1988 2014-07-05
  • 打赏
  • 举报
回复
String UserName = UserNameEditText.getText().toString().trim(); 
                    String Pwd = PassEditText.getText().toString().trim();                   
                    TextViewResult.setText("用户名" + UserName  + "," + "密码" + Pwd);
                     
                    Thread th= new Thread(new Runnable() {
 
                        @Override
                        public void run() {
                         
                            getRemoteInfo(UserName,Pwd);
 
                        }
                        });
                     
                    th.start();
你确定这段能编译通过?

80,359

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧