2012-10-26 2 views
1

내 인생에서 제 코드에 무엇이 잘못되었는지를 알 수 없습니다.startActivity (intent)의 NullPointerException

AgrUserLoginHome.java 
     public void onClick(View v) 
     { 
    if(v.getId()==R.id.btnSubmitLogin) 
     { 
    String username=txtUsername.getText().toString(); 
    String password=txtPassword.getText().toString(); 
    //this is passed to be used in next LoginClientServiceThread class 
    LoginClientServiceThread verifyLog = new 
      LoginClientServiceThread(this,username,password); 
     verifyLog.run(); 

     } 
    } 

LoginClientServiceThread.java는

public class LoginClientServiceThread extends Activity implements Runnable{ 

private Intent intent; 
private String username,password; 
private Context context; 
public LoginClientServiceThread(Context cont,String user,String pass) 
{ 
    username=user; 
    password=pass; 
    context=cont; 
} 
public void run() { 
    try 
    { 
     try 
     { 
      JSONObject json = new JSONObject(); 
      json.put("username", username); 
      json.put("password", password); 

      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      URI uri = new URI("http://10.0.2.2:80/BairyApp/Login.svc"); 

      HttpPost httppost = new HttpPost(uri + "/verifyLogin"); 
      httppost.setHeader("Accept", "application/json"); 
      httppost.setHeader("Content-type", "application/json; charset=utf-8"); 

      HttpResponse response = httpClient.execute(httppost); 
      HttpEntity responseEntity = response.getEntity(); 

      long intCount = responseEntity.getContentLength(); 
      char[] buffer = new char[(int)intCount]; 
      InputStream stream = responseEntity.getContent(); 
      InputStreamReader reader = new InputStreamReader(stream, "UTF-8"); 

      try 
      { 
       reader.read(buffer); 
       String str = new String(buffer); 
       intent=new Intent(context,TrainerAccessScreen.class); 
       intent.putExtra("reading",str); 
       if(null!=intent) 
       { 
        Toast.makeText(context, "Working", 
          Toast.LENGTH_LONG).show(); 
          //startActivity shows as null here 
        startActivity(intent); 
       } 
       Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
       Toast.makeText(context, "IOException:"+e.getMessage(), 
        Toast.LENGTH_LONG).show(); 
       } 
       finally 
      { 
       stream.close(); 
      } 

     } 
     catch(URISyntaxException urise) 
     { 
      urise.printStackTrace(); 
      Toast.makeText(context, "urise", Toast.LENGTH_LONG).show(); 
     } 
     catch(ClientProtocolException cpe) 
     { 
      cpe.printStackTrace(); 
      Toast.makeText(context, "cpe", Toast.LENGTH_LONG).show(); 
     } 
     catch(IOException ioe) 
     { 
      ioe.printStackTrace(); 
      Toast.makeText(context, "ioe", Toast.LENGTH_LONG).show(); 
     } 
    } 
    catch(Exception e) 
    { 
     Toast.makeText(context, "Execution failed: "+e.getMessage(), 
        Toast.LENGTH_SHORT).show(); 
     e.printStackTrace(); 
    } 
    } 
    } 

나는 startActivity를 방법은 널을 반환 확신합니다. 나는 이것을 알기 때문에 int 문맥을 "this"로 변경하면 바로 아래의 IOException에있는 토스트가 표시되지 않기 때문입니다.

아이디어가 있으십니까? 내가 코드 당신은 LoginClientServiceThreadActivity이 아니라고, 그러나 당신은 Activity을 확장

10-26 20:28:48.756: I/dalvikvm(1133): Jit: resizing JitTable from 512 to 1024 
10-26 20:41:57.906: W/System.err(1169): java.lang.NullPointerException 
10-26 20:41:57.906: W/System.err(1169):  at android.app.Activity.startActivityForResult(Activity.java:2827) 
10-26 20:41:57.906: W/System.err(1169):  at android.app.Activity.startActivity(Activity.java:2933) 
10-26 20:41:57.916: W/System.err(1169):  at agr.mobile.service.LoginClientServiceThread.run(LoginClientServiceThread.java:76) 
10-26 20:41:57.916: W/System.err(1169):  at agr.mobile.app.AgrUserLoginHome.onClick(AgrUserLoginHome.java:59) 
10-26 20:41:57.916: W/System.err(1169):  at android.view.View.performClick(View.java:2485) 
10-26 20:41:57.916: W/System.err(1169):  at android.view.View$PerformClick.run(View.java:9080) 
10-26 20:41:57.916: W/System.err(1169):  at android.os.Handler.handleCallback(Handler.java:587) 
10-26 20:41:57.916: W/System.err(1169):  at android.os.Handler.dispatchMessage(Handler.java:92) 
10-26 20:41:57.916: W/System.err(1169):  at android.os.Looper.loop(Looper.java:123) 
10-26 20:41:57.916: W/System.err(1169):  at android.app.ActivityThread.main(ActivityThread.java:3683) 
10-26 20:41:57.916: W/System.err(1169):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-26 20:41:57.916: W/System.err(1169):  at java.lang.reflect.Method.invoke(Method.java:507) 
10-26 20:41:57.916: W/System.err(1169):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
10-26 20:41:57.926: W/System.err(1169):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
10-26 20:41:57.926: W/System.err(1169):  at dalvik.system.NativeStart.main(Native Method) 
+2

logcat 출력을 넣어주세요 – hshed

+0

매니페스트에이 클래스가 있습니까? –

+0

LoginClientServiceThread.java 파일은 활동 파일이 아닙니다. 그것은 활동 클래스 외부에서 서비스 작업을 수행하기위한 Java 클래스입니다. – Zubair

답변

4

끔찍한

로그 캣을 보이는 알 수 있도록 BTW 오, 난 초보자입니다. 이러지 마.

intent=new Intent(context,TrainerAccessScreen.class); 
intent.putExtra("reading",str); 
if(null!=intent) 
{ 
    Toast.makeText(context, "Working", 
    Toast.LENGTH_LONG).show(); 
    context.startActivity(intent); 
} 

당신이 LoginClientServiceThreadthis 인스턴스에서 startActivity()를 호출하고 같이가 NullPointerException가 있어요 : 당신이 비 활동 클래스에서 Activity를 시작하려면 다음 사용합니다. 그러나 해당 활동을 시작하지 않고 정상 활동으로 작성 했으므로 널 참조가 있습니다. 두 번째 클래스의 모든 Activity을 확장 할 필요가 없습니다

public class LoginClientServiceThread implements Runnable{ 

    private Intent intent; 
    private String username,password; 
    private Context context; 
    public LoginClientServiceThread(Context cont,String user,String pass) 
    { 
     username=user; 
     password=pass; 
     context=cont; 
    } 

    public void run() { 
     try 
     { 
      try 
      { 
       JSONObject json = new JSONObject(); 
       json.put("username", username); 
       json.put("password", password); 

       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       URI uri = new URI("http://10.0.2.2:80/BairyApp/Login.svc"); 

       HttpPost httppost = new HttpPost(uri + "/verifyLogin"); 
       httppost.setHeader("Accept", "application/json"); 
       httppost.setHeader("Content-type", "application/json; charset=utf-8"); 

       HttpResponse response = httpClient.execute(httppost); 
       HttpEntity responseEntity = response.getEntity(); 

       long intCount = responseEntity.getContentLength(); 
       char[] buffer = new char[(int)intCount]; 
       InputStream stream = responseEntity.getContent(); 
       InputStreamReader reader = new InputStreamReader(stream, "UTF-8"); 

       try 
       { 
        reader.read(buffer); 
        String str = new String(buffer); 
        intent=new Intent(context,TrainerAccessScreen.class); 
        intent.putExtra("reading",str); 
        if(null!=intent) 
        { 
         Toast.makeText(context, "Working", 
          Toast.LENGTH_LONG).show(); 
         //startActivity shows as null here 
         context.startActivity(intent); 
        } 
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); 
       } 
       catch (IOException e) 
       { 
        e.printStackTrace(); 
        Toast.makeText(context, "IOException:"+e.getMessage(), 
         Toast.LENGTH_LONG).show(); 
       } 
       finally 
       { 
        stream.close(); 
       } 

      } 
      catch(URISyntaxException urise) 
      { 
       urise.printStackTrace(); 
       Toast.makeText(context, "urise", Toast.LENGTH_LONG).show(); 
      } 
      catch(ClientProtocolException cpe) 
      { 
       cpe.printStackTrace(); 
       Toast.makeText(context, "cpe", Toast.LENGTH_LONG).show(); 
      } 
      catch(IOException ioe) 
      { 
       ioe.printStackTrace(); 
       Toast.makeText(context, "ioe", Toast.LENGTH_LONG).show(); 
      } 
     } 
     catch(Exception e) 
     { 
      Toast.makeText(context, "Execution failed: "+e.getMessage(), 
       Toast.LENGTH_SHORT).show(); 
      e.printStackTrace(); 
     } 
    } 
} 

: 같은 최종 코드를 찾을 것입니다.

+0

감사합니다 !! 매력처럼 작동합니다 : D – Zubair

+0

다행입니다. :) –