2015-11-16 47 views
0

我是新来的android和翻新。当我点击登录时,如果回调返回“成功登录”消息,那么我应该重定向到主页,但似乎我不允许在回调函数内部调用intent。不能调用意图内部翻新成功回调函数

public void loginUser() { 
    //Here we will handle the http request to insert user to mysql db 
    //Creating a RestAdapter 
    RestAdapter adapter = new RestAdapter.Builder() 
      .setEndpoint(ROOT_URL) //Setting the Root URL 
      .build(); //Finally building the adapter 

    //Creating object for our interface 
    LoginAPI api = adapter.create(LoginAPI.class); 

    api.loginUser(

      //Passing the values by getting it from editTexts 
      editTextUsername.getText().toString(), 
      editTextPassword.getText().toString(), 

      //Creating an anonymous callback 
      new Callback<Response>() { 
       @Override 
       public void success(Response result, Response response) { 
        //On success we will read the server's output using bufferedreader 
        //Creating a bufferedreader object 
        BufferedReader reader = null; 

        //An string to store output from the server 
        String output = ""; 

        try { 
         //Initializing buffered reader 
         reader = new BufferedReader(new InputStreamReader(result.getBody().in())); 

         //Reading the output in the string 
         output = reader.readLine(); 

         if(output == "Successful login") { 
          Intent i = new Intent(this, Home.class); 
          startActivity(i); 
         } 
         //Log.d(TAG, output); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 

        //Displaying the output as a toast 
        Toast.makeText(Login.this, output, Toast.LENGTH_LONG).show(); 

        /*SharedPreferences mSettings = getSharedPreferences("data", 0); 
        SharedPreferences.Editor editor = mSettings.edit(); 
        editor.putString("callback", output); 
        editor.commit();*/ 

       } 

       @Override 
       public void failure(RetrofitError error) { 
        //If any error occured displaying the error as toast 
        Toast.makeText(Login.this, error.toString(), Toast.LENGTH_LONG).show(); 
       } 
      } 

    ); 
} 
+0

此代码位于何处?它是在一个活动中还是在一个单独的类中? –

+0

它在登录活动中。 – rendell

回答

1

您正在使用错误的指针。

Intent i = new Intent(this, Home.class); 

你应该用你的活动的这个指针,不是你的匿名内部类的一个。例如。如果您的活动被命名为登录,您应该按照以下方式构建您的意图:

Intent i = new Intent(Login.this, Home.class); 
+0

谢谢。我需要7分钟才能接受答案。 – rendell