2017-06-04 43 views
0

开始新的活动我有简单的问题,我觉得简直就是:) 我无法从我的改造回调方法开始新的活动,我越来越喜欢改造1.9如何从回调

问题“ android.util.AndroidRuntimeException:从Activity上下文外调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志,这真的是你想要的吗?

sign_in_button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      String userEmail = email.getText().toString(); 
      String userPassword = password.getText().toString(); 

      final LoginService loginService = RetrofitClient.createService(LoginService.class, userEmail, userPassword); 
      loginService.basicLogin(new Callback<User>() { 
       @Override 
       public void success(User user, Response response) { 
        Toast.makeText(getApplicationContext(),"You are Logged", Toast.LENGTH_SHORT).show(); 

        Intent intent = new Intent(getApplicationContext(), BetweenActivity.class); 
        startActivity(intent); 





       } 

       @Override 
       public void failure(RetrofitError error) { 
        Toast.makeText(getApplicationContext(),"Something went wrong", Toast.LENGTH_SHORT).show(); 

       } 
      }); 

这是我的日志:

致命异常:主要 过程:com.example.bunzlau.retrofit,PID:18138 了java.lang.RuntimeException:无法启动活动ComponentInfo {融为一体。 example.bunzlau.retrofit/com.example.bunzlau.retrofit.BetweenActivity}:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.view.View android.widget.Button.findViewById(int)' at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) at android.app.ActivityThread.access $ 1100(ActivityThread.java:221) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1794 )android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7225) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230) 在com.android.internal.os.ZygoteInit.main( ž ygoteInit.java:1120) 引起:java.lang.NullPointerException:尝试在null对象引用 上的com.example.bunzlau上调用虚拟方法'android.view.View android.widget.Button.findViewById(int)' .retrofit.BetweenActivity.onCreate(BetweenActivity.java:18) at android.app.Activity.performCreate(Activity.java:6876) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135) at android.app (ActivityThread.java:220) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) (android.os.Load.loop)(Looper.java:158)在android.app.ActivityThread上的$ H.handleMessage(ActivityThread.java:1794) (android.os.Handler.dispatchMessage(Handler.java:102) ) at android.app.ActivityThread.main(ActivityThread.java:7225) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java :1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

+0

看来你的BetweenActivity。onCreate()抛出异常,所以你的问题可能不会直接关系到从Retrofit开始一个活动。 在BetweenActivity.onCreate()中查找findViewById()。 – Vaiden

回答

0

您从回调的范围内调用startActivity(),而不是t他活动的范围。您需要获取对当前活动的参考,然后使用它开始下一个活动。

private Context context = this; 

sign_in_button.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     String userEmail = email.getText().toString(); 
     String userPassword = password.getText().toString(); 

     final LoginService loginService = RetrofitClient.createService(LoginService.class, userEmail, userPassword); 
     loginService.basicLogin(new Callback<User>() { 
      @Override 
      public void success(User user, Response response) { 
       Toast.makeText(getApplicationContext(),"You are Logged", Toast.LENGTH_SHORT).show(); 

       Intent intent = new Intent(getApplicationContext(), BetweenActivity.class); 
       context.startActivity(intent); 





      } 

      @Override 
      public void failure(RetrofitError error) { 
       Toast.makeText(getApplicationContext(),"Something went wrong", Toast.LENGTH_SHORT).show(); 

      } 
     }); 
+0

感谢您的评论,但我仍然收到此错误:“由:java.lang.NullPointerException引发”。你知道也许有什么理由吗? –

+0

我的意思是,我正在参考当前的活动,但它无法正常工作。 –