2013-11-25 32 views
-1

我有一個註冊系統。註冊工作正常。我的主要問題是:我想在登錄後啓動MainActivity.java。在將登錄數據發送到服務器後,服務器檢查數據庫是否匹配併發送一個int(0表示不匹配)和(1表示成功) 。這也很好。但是,如果我要開始後onPostExecute法的意圖它給出了一個錯誤:Android onpostexecute無法啓動後的意圖

FATAL EXCEPTION: main java.lang.NullPointerException at android.app.Activity.startActivityForResult ...

這是我的起始頁其中exectues我的AsyncTask類。並在getLoginMessage()方法中獲得成功或無法匹配。

public class LoginPage extends Activity { 

String userName; 
String password; 
String sendProtocolToServer; 
static String matched = null; 
static String unmatched; 
static Context myCtx; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_loginpage); 

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

    Button login = (Button) findViewById(R.id.loginBtn); 
    login.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      handleLogin(); 
     } 
    }); 

    Button register = (Button) findViewById(R.id.registerBtn); 
    register.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Intent openMainActivityRegister = new Intent(
        "com.example.fotosharing.REGISTERPAGE"); 
      startActivity(openMainActivityRegister); 
     } 
    }); 

} 

private void handleLogin() { 

    EditText editTextBox = (EditText) findViewById(R.id.EditTextUser); 
    EditText passwordTextBox = (EditText) findViewById(R.id.EditTextPassword); 
    userName = editTextBox.getText().toString(); 
    password = passwordTextBox.getText().toString(); 

    if (!userName.equals("") && !password.equals("")) { 
     sendProtocolToServer = "login" + "#" + userName + "#" + password; 
     ConnectToServer cts = new ConnectToServer(sendProtocolToServer); 

     cts.execute(); 
    } else { 
     Toast.makeText(this, "Fill in Username and Password to login", 
       Toast.LENGTH_LONG).show(); 
    } 

} 

public void getLoginMessage(String receivedMessage) { 

    if (receivedMessage.equals("success")) { 
     Intent openMainActivity = new Intent(
       "com.example.fotosharing.TIMELINEACTIVITY"); 
     openMainActivity.clone(); 
     startActivity(openMainActivity); 

    } 

    if (receivedMessage.equals("unmatched")) { 
     Toast.makeText(this, "Password or username incorrect.", Toast.LENGTH_LONG).show(); 
    } 
} 

} 

這是我的Async-Task類,它從我的Java-Server接收數據,並檢查它是否成功或不匹配的登錄。在onPostExecute即時調用LoginPage.class中的一個方法,它處理意圖(這裏是錯誤)。

public class ConnectToServer extends AsyncTask<Void, Void, String> { 
    public Context myCtx; 
    static Socket socket; 
    String sendStringToServer; 
    int protocolId = 0; 
    private static DataOutputStream DOS; 
    private static DataInputStream DIS; 
    StringBuffer line; 
    int j = 1; 
    String value; 
    static String res = null; 

public ConnectToServer(String sendStringToServer) { 
    this.sendStringToServer = sendStringToServer; 
} 

public ConnectToServer(int i) { 
    this.protocolId = i; 
} 

public ConnectToServer() { 

} 

public ConnectToServer(Context ctx) { 
    this.myCtx = ctx; 
} 

protected String doInBackground(Void... arg0) { 

    try { 
     socket = new Socket("192.168.1.106", 25578); 
     DOS = new DataOutputStream(socket.getOutputStream()); 

     if (protocolId == 1) { 
      DOS.writeUTF("pictureload"); 
      protocolId = 0; 

     } else { 
      DOS.writeUTF(sendStringToServer); 

     } 

     res = receive(); 
     // DOS.close(); 
    } catch (UnknownHostException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    System.out.println("RES: " + res); 

    return res; 
} 

public String receive() { 
    String receiveResult = null; 

    if (socket.isConnected()) { 

     try { 
      BufferedReader input = new BufferedReader(
        new InputStreamReader(socket.getInputStream())); 

      DIS = new DataInputStream(socket.getInputStream()); 
      int msg_received = DIS.readInt(); 

      System.out.println("SERVER: " + msg_received); 

      if (msg_received == 1) { 
       receiveResult = "success"; 
       System.out.println("IF (success): " + receiveResult); 

      } 

      if (msg_received == 0) { 
       receiveResult = "unmatched"; 
       System.out.println("ELSE IF (unmatched): " 
         + receiveResult); 

      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    // ***** return your accumulated StringBuffer as string, not current 
    // line.toString(); 
    return receiveResult; 

} 

protected void onPostExecute(String result1) { 

    if (result1 != null) { 
     if (result1.equals("success") || result1.equals("unmatched")) { 
      sendToLoginPage(result1); 
     } 
    } 
} 

private void sendToLoginPage(String result1) { 
    System.out.println("sendtologi " + result1); 
    LoginPage lp = new LoginPage(); 
    lp.getLoginMessage(result1); 
} 

} 

這是我想在成功登錄時啓動的類。 我在做什麼錯?

public class MainActivity extends SherlockFragmentActivity { 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

    ActionBar actionbar = getSupportActionBar(); 
    actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
    actionbar.setBackgroundDrawable(new ColorDrawable(Color.BLACK));   
    actionbar.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#91d100"))); 

    ActionBar.Tab Frag1Tab = actionbar.newTab().setText("Home"); 
    ActionBar.Tab Frag2Tab = actionbar.newTab().setText("Share Photo"); 

    Fragment Fragment1 = new TimelineActivity(); 
    Fragment Fragment2 = new CameraActivity(); 

    Frag1Tab.setTabListener(new MyTabsListener(Fragment1)); 
    Frag2Tab.setTabListener(new MyTabsListener(Fragment2)); 

    actionbar.addTab(Frag1Tab); 
    actionbar.addTab(Frag2Tab); 
} 
} 
+0

您可以包括完整的堆棧跟蹤? –

+0

您只需調用Activity構造函數即可創建自己的LoginPage對象。 Android會爲你打電話。你所要做的只是實現回調。你需要閱讀一些活動和片段...... – gunar

回答

0

你不能只是創建activitynew關鍵字的情況下,像這樣:

private void sendToLoginPage(String result1) { 
    System.out.println("sendtologi " + result1); 
    LoginPage lp = new LoginPage(); 
    lp.getLoginMessage(result1); 
} 

這是錯誤,它可能是爲什麼您收到錯誤。您發佈的代碼非常複雜,所以我不確定是否還有其他問題。

這是應該怎麼做:

所以...因爲你很可能有ConnectToServer的AsyncTask單獨的文件中,你需要通過事件或數據傳輸到LoginPage活動。爲此,您應該使用事件監聽器,如下所示:

首先,創建將代表您的ConnectToServerLoginPage之間通信的接口。現在

public interface LoginResultListener { 
    public void getLoginMessage(String receivedMessage); 
} 

,讓你的LoginPageactivity實現這個interface

public class LoginPage extends Activity implements LoginResultListener { 
... 
} 

現在,更新ConnectToServer的AsyncTask,以便它使用LoginResultListener到登錄結果傳達給你的活動,如:

public class ConnectToServer extends AsyncTask<Void, Void, String> { 

    private LoginResultListener listener; 
... 
    public void setListener(LoginResultListener listener) { 
    this.listener = listener; 
    } 
... 
    private void sendToLoginPage(String result1) { 
    System.out.println("sendtologi " + result1); 
    //THIS IS WHERE YOU DID WRONG 
    listener.getLoginMessage(result1); 
    } 
... 
} 

現在終於,當你創建new ConnectToServer。從你的行爲,你需要設置監聽器將處理事件時,在用戶登錄既然你實現你的活動這個界面中,你會送你的活動對象爲收聽參數,見下圖:

ConnectToServer cts = new ConnectToServer(sendProtocolToServer); 
// THIS IS IMPORTANT PART - 'this' refers to your LoginPage activity, that implements LoginResultListener interface 
cts.setListener(this); 
cts.execute(); 
+0

上面的方法正確地將數據發送到LoginPage。但是,如果我嘗試啓動意圖失敗。你能否提出一個建議如何做到這一點? –

+0

@ charly_freak35請花點時間仔細閱讀我的文章。 – hendrix

+0

非常感謝!將試試:) –