当我运行我的应用程序时,我有一个异常显示,所以我想帮助我:)异常是:java。lang.NullPointerException:尝试调用虚拟方法“int java”。lang.String。空对象引用上的长度()
当我单击Login类中的dd1按钮和按钮实现(public void getData(View v))方法时,会显示异常
非常感谢。
登录类
public class Login extends Activity {
Button DD1;
TextView r;
EditText PhoneNumber1, Password1;
ProgressBar PB;
private RequestPackage p;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
r = (TextView) findViewById(R.id.message);
PhoneNumber1 = (EditText) findViewById(R.id.PN_login);
Password1 = (EditText) findViewById(R.id.PW_login);
DD1 = (Button) findViewById(R.id.loginButton);
PB = (ProgressBar) findViewById(R.id.pro);
PB.setVisibility(View.INVISIBLE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void getData(View v) {
PB.setVisibility(View.VISIBLE);
RequestPackage p = new RequestPackage();
p.setMethod("GET");
p.setUri("http://9aleh.com/feed.asmx/CheckAccountWithPassword");
p.setParams("phoneNumber", PhoneNumber1.getText().toString());
p.setParams("password", Password1.getText().toString());
String content = HttpManager.getData(p);
if (AccountXMLParser.parseFeedChecklogin(content)) {
r.setText("TRUE");
}else
r.setText("FALSE");
PB.setVisibility(View.INVISIBLE);
}
}
HttpManager类
public class HttpManager {
public static String getData(RequestPackage p) {
BufferedReader reader = null;
String uri = p.getUri();
if(p.getMethod().equals("GET")){
uri+= "?"+p.getEncodeParams();
}
try {
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod(p.getMethod());
StringBuilder sb = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line ;
int fl=0;
while ((line = reader.readLine()) != null) {
if(fl!= 0)
sb.append(line + " \n");
fl++;
}
reader.close();
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if ((reader != null)) {
}
try {
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
日志:
10-02 03:19:48.485 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
10-02 03:19:48.489 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
10-02 03:19:48.498 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
10-02 03:19:48.498 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:215)
10-02 03:19:48.498 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
10-02 03:19:48.498 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
10-02 03:19:48.498 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
10-02 03:19:48.498 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
10-02 03:19:48.498 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
10-02 03:19:48.498 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
10-02 03:19:48.498 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
10-02 03:19:48.498 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
10-02 03:19:48.522 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
10-02 03:19:48.522 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)
10-02 03:19:48.522 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:231)
10-02 03:19:48.522 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.example.salehabdarhman.ex.HttpManager.getData(HttpManager.java:27)
10-02 03:19:48.522 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.example.salehabdarhman.ex.Login.getData(Login.java:72)
10-02 03:19:48.524 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
10-02 03:19:48.524 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
10-02 03:19:48.524 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View.performClick(View.java:5198)
10-02 03:19:48.525 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View$PerformClick.run(View.java:21147)
10-02 03:19:48.525 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
10-02 03:19:48.525 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
10-02 03:19:48.525 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Looper.loop(Looper.java:148)
10-02 03:19:48.525 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5417)
10-02 03:19:48.525 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
10-02 03:19:48.526 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-02 03:19:48.526 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-02 03:19:48.527 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.BufferedReader.close()' on a null object reference
10-02 03:19:48.527 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.example.salehabdarhman.ex.HttpManager.getData(HttpManager.java:45)
10-02 03:19:48.527 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.example.salehabdarhman.ex.Login.getData(Login.java:72)
10-02 03:19:48.527 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
10-02 03:19:48.548 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
10-02 03:19:48.548 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View.performClick(View.java:5198)
10-02 03:19:48.551 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View$PerformClick.run(View.java:21147)
10-02 03:19:48.551 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
10-02 03:19:48.552 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
10-02 03:19:48.553 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Looper.loop(Looper.java:148)
10-02 03:19:48.553 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5417)
10-02 03:19:48.553 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
10-02 03:19:48.553 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-02 03:19:48.554 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-02 03:19:48.556 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
10-02 03:19:48.556 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.io.StringReader.<init>(StringReader.java:47)
10-02 03:19:48.556 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.example.salehabdarhman.ex.AccountXMLParser.parseFeedChecklogin(AccountXMLParser.java:77)
10-02 03:19:48.556 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.example.salehabdarhman.ex.Login.getData(Login.java:73)
10-02 03:19:48.561 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
10-02 03:19:48.561 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
10-02 03:19:48.561 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View.performClick(View.java:5198)
10-02 03:19:48.561 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View$PerformClick.run(View.java:21147)
10-02 03:19:48.561 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
10-02 03:19:48.562 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
10-02 03:19:48.562 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Looper.loop(Looper.java:148)
10-02 03:19:48.562 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5417)
10-02 03:19:48.563 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
10-02 03:19:48.563 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-02 03:19:48.563 10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
看起来是一个相对简单的错误。在你的HttpManager类的最后一个块。
应该是
} finally {
if ((reader != null)) {
try {
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
但是,我强烈建议,如果您使用的是Java7,请使用try with参考资料。
因此,您的getData方法将成为
public static String getData(RequestPackage p) {
= null;
String uri = p.getUri();
if(p.getMethod().equals("GET")){
uri+= "?"+p.getEncodeParams();
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod(p.getMethod());
StringBuilder sb = new StringBuilder();
String line ;
int fl=0;
while ((line = reader.readLine()) != null) {
if(fl!= 0)
sb.append(line + " \n");
fl++;
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
这要容易得多,并且您不必担心关闭资源,因为它们实现了AutoCloseable。
问题内容: 我正在尝试获取一些信息,但我的应用程序崩溃并显示以下消息: 这是代码: 这是一个纯课。的哪个调用不包含this 。我做错了吗?应始终位于内吗? 根据要求整体: 问题答案: 改成 您为布局充气。视图属于膨胀的布局。因此,使用view对象在中初始化视图 片段由活动主持 您可以在视图中使用和初始化 还在中初始化TextView 。由于Asynctask是一个内部类,因此您可以在那里更新ui
问题内容: Process: com.example.vicevirus.myapplication, PID: 13471 java.lang.NullPointerException: Attempt to invoke virtual method ‘java.lang.String com.google.firebase.auth.FirebaseUser.getUid()’ on a n
在为我的CardView设置onclick listner时获得Nullpointer异常。 错误为: 原因:java.lang.NullPointerException:试图对com.jaysurya.project_scrollwithTap.mainactivity.onCreate(mainactivity.java:27)处的空对象引用调用虚拟方法“void android.suppor
每当我按下应用程序中的登录按钮时,就会出现此错误。有什么建议吗? java.lang.NullPointerExcture:尝试在com.example.android.login.LoginActivity.is空(LoginActivity.java:93)在com.example.android.login.登录ctivity.access0美元(登录ctivity.java:20)在com
每当我尝试使用firebase auth注册用户时,我的应用程序就会崩溃。我的代码如下: