错误: 找不到符号 符号: 变量 handler ,是哪里错了啊,小白求助~~!
package com.example.myapplication;
import android.content.Intent;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.lang.reflect.Method;
public class LoginActivity extends AppCompatActivity {
private EditText editTextUid;
private EditText editTextPwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
editTextUid = (EditText) findViewById(R.id.EditTextuid);
editTextPwd = (EditText) findViewById(R.id.EditTextPwd);
Button loginbutton = (Button) findViewById(R.id.loginBtn);
loginbutton.setOnClickListener(this.LoginInButtonlistener);
WorkThread wt = new WorkThread();
wt.start();
}
View.OnClickListener LoginInButtonlistener = new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = editTextUid.getText().toString();//获取页面密码
String sy = editTextPwd.getText().toString();//获取页面用户名
Message m = handler.obtainMessage();
Bundle b = new Bundle();
b.putString("pass", s);//以键值对形式放进 Bundle中
b.putString("name", sy);
m.setData(b);
handler.sendMessage(m);//把信息放到通道中
}
};
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
if (menu != null) {
if (menu.getClass().getSimpleName().equalsIgnoreCase("MenuBuilder")) {
try {
Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
method.setAccessible(true);
method.invoke(menu, true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return super.onMenuOpened(featureId, menu);
}
class WorkThread extends Thread{
@Override
public void run(){
Looper.prepare();
handler=new Handler(){
@Override
public void handleMessage(Message m){
super.handleMessage(m);
Bundle b= m.getData();//得到与信息对用的Bundle
String name=b.getString("name");//根据键取值
String pass=b.getString("pass");
DBUtil db=new DBUtil(name,pass);//调用数据库查询类
String ret=db.QuerySQL();//得到返回值
if (ret.equals("1"))//为1,页面跳转,登陆成功
{
Intent localIntent = new Intent();
localIntent.setClass(LoginActivity.this,MainActivity.class);
LoginActivity.this.startActivity(localIntent);
Toast.makeText(LoginActivity.this, "登录成功",Toast.LENGTH_SHORT).show();//显示提示框
return;
}
Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
}
};
Looper.loop();//Looper循环,通道中有数据执行,无数据堵塞
}
}
}