java.lang.NullPointerException当我试图使按钮不可见时

时间:2014-10-10 13:23:40

标签: android button android-activity nullpointerexception

此方法正常。

public void onClick(View view) { 
btn = (Button) findViewById(R.id.trytogetin);
progr = (ProgressBar) findViewById(R.id.progressBar);
btn.setVisibility(View.INVISIBLE);
progr.setVisibility(View.VISIBLE);
EditText login = (EditText) findViewById(R.id.loginfld);
EditText passw = (EditText) findViewById(R.id.passfld);
String logincmd = "CheckLogin*" + login.getText() + "*" + passw.getText() + "*";
ss.senddata(logincmd, 1);
}

在此方法中出现java.lang.NullPointerException(在btn.setVisibility(View.VISIBLE)上;

    public void geturdata(String answer) {
    if (answer != null)
    {
        System.out.println("true");
         btn.setVisibility(View.VISIBLE);
    }
    else
    {
        System.out.println("false");
    }
}

请告诉我如何在此方法中拨打按钮?此外,我无法在此方法中使用StartActivity(intent)。(相同的错误)。两个方法都放在一个活动中。

这是完整的代码 这是活动

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;


public class Login extends Activity {

SocketServer ss = new SocketServer();
Button btn;
ProgressBar progr;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    btn = (Button) findViewById(R.id.trytogetin);
    progr = (ProgressBar) findViewById(R.id.progressBar);
    try {
        ss.setserver();
    } catch (Exception ex) {
        System.out.println("------- " + ex);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    menu.add("menu1");
    getMenuInflater().inflate(R.menu.login, 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);
 }

 public void onClick(View view) {

    btn.setVisibility(View.INVISIBLE);
    progr.setVisibility(View.VISIBLE);
    EditText login = (EditText) findViewById(R.id.loginfld);
    EditText passw = (EditText) findViewById(R.id.passfld);
    String logincmd = "CheckLogin*" + login.getText() + "*" + passw.getText() + "*";
    ss.senddata(logincmd, 1);
 }

    public void geturdata(String answer) {
    if (answer != null)
    {
        System.out.println("true");
        btn.setVisibility(View.VISIBLE);
    }
    else
    {
        System.out.println("false");
    }
}
}

这是调用> geturdata

的类
import android.os.Looper;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketServer {

private Socket socket;
private static final int SERVERPORT = 11000;
private static String SERVER_IP = "192.168.2.222";
String answer;
private static String cmdtext;
private static int caller;

class ClientThread implements Runnable
{
   public void run() {
       if (cmdtext.equals("setserver"))
       {
           try
           {
               InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
               socket = new Socket(serverAddr, SERVERPORT);
           }
           catch (Exception ex)
           {System.out.println(ex);}
       }

       else {
           try {
               String str = cmdtext;
               PrintWriter out = new PrintWriter(new BufferedWriter(new       OutputStreamWriter(socket.getOutputStream())), true);
               out.println(str);
               out.flush();
               byte[] data = new byte[256];  
               InputStream inp = socket.getInputStream();
               inp.read(data);
               answer = new String(data, "UTF-8");
                                  Looper.prepare();
               handledata(answer);
               Looper.loop();
           } catch (UnknownHostException e) {
               e.printStackTrace();
           } catch (IOException e) {
               e.printStackTrace();
           } catch (Exception e) {
               e.printStackTrace();
           }
       }
    }
}
////////////////////////////////////////
public void setserver ()
{
   new Thread(new ClientThread()).start();
   cmdtext = "setserver";
}

private void handledata(String answer)  
{
    switch (caller) {
        case 1:
        {
            Login lgn = new Login();
            lgn.geturdata(answer);
        }
    }

}

public void senddata(String cmd, int callerid) 
{
this.cmdtext = cmd;
this.caller = callerid;
new Thread(new ClientThread()).start();
}
}

3 个答案:

答案 0 :(得分:0)

致电btn时,

nullgeturdata()。您应该在btn = (Button) findViewById(R.id.trytogetin);

中的setContentView(R.layout.activity_main);后直接致电public void onCreate(Bundle b)
private Button btn;  

public void onCreate(Bundle b) {
    super.onCreate(b);
    setContentView(R.layout.activity_main);
    btn = (Button)findViewById(R.id.trytogetin);
    ...
}

编辑:

但是,现在看到你的代码,那不是问题。这里有两个问题

        Login lgn = new Login();
        lgn.geturdata(answer);

您尝试实例化Activity。您应该通过意图开始活动,以便在正常的活动生命周期中实现它们:通过onCreate

但还有更多:您尝试将Button设置为另一个Thread,而不是创建Button(UI线程)的context.runOnUiThread(Runnable)

您可以尝试从线程中的上下文获取句柄,然后调用NPE。这样您就可以从其他线程更新UI。

所以Login是由于你实例化Android(必须由findViewById完成)并调用一个方法,但是没有ContentView膨胀,所以无论如何都找不到按钮,{{1}}返回null

答案 1 :(得分:0)

在OnCreate()

中声明
btn = (Button) findViewById(R.id.trytogetin); 

progr =(ProgressBar)findViewById(R.id.progressBar);

public void onClick(View view) { 
btn.setVisibility(View.INVISIBLE);
 progr.setVisibility(View.VISIBLE);
   }
  String logincmd = "CheckLogin*" + login.getText().toString() + "*" + passw.getText().toString() + "*";

所以引用这个方法geturdata你使用Button btn的对象返回null,因为你在onClick按钮里面使用了。

 public void geturdata(String answer) {

答案 2 :(得分:0)

您在点击方法中获得了按钮的ID。因此,要解决此问题,请在oncreate()方法中获取您的按钮ID。所以你可以随时随地访问按钮。但声明按钮全局