2015-01-21 66 views
1

在將此問題放在此處之前,我嘗試了很多。方法調用在Android中不起作用

Messaging msg = new Messaging(); 

msg.appendToMessageHistorysend(FriendList.ownusername, str); 

我打電話appendToMessageHistorysend功能從另一個活動,以消息類

public void appendToMessageHistorysend(String username, String message) { 
     if (username != null && message != null) { 

      username1 = "<font color=#FF66CC>" + "<b>" + username + "<b/>" 
        + ": " + "</font>"; 

      message1 = "<font color=#ffcc00>" + message + "&#13;&#10;" 
        + "<br/>" + "</font>"; 

      Thread thread = new Thread() { 

       @Override 
       public void run() { 

        handler.post(new Runnable() { 

         public void run() { 
            if (messageHistoryText != null) { 
          messageHistoryText.append(Html 
            .fromHtml(username1)); 
          messageHistoryText.append(Html 
            .fromHtml(message1)); 
         } 

         else { 
          Toast.makeText(mCon, "hey yo you are null", 
            Toast.LENGTH_LONG).show(); 
         } 
         } 

        }); 
       } 

      }; 
      thread.start(); 

     } 
    } 

這裏是調用的函數和我收到錯誤,如下面

01-21 15:21:42.552: E/AndroidRuntime(16751): FATAL EXCEPTION: main 
01-21 15:21:42.552: E/AndroidRuntime(16751): java.lang.NullPointerException 
01-21 15:21:42.552: E/AndroidRuntime(16751): at at.vcity.androidim.Messaging$7$1.run(Messaging.java:515) 
01-21 15:21:42.552: E/AndroidRuntime(16751): at android.os.Handler.handleCallback(Handler.java:615) 
01-21 15:21:42.552: E/AndroidRuntime(16751): at android.os.Handler.dispatchMessage(Handler.java:92) 
01-21 15:21:42.552: E/AndroidRuntime(16751): at android.os.Looper.loop(Looper.java:137) 
01-21 15:21:42.552: E/AndroidRuntime(16751): at android.app.ActivityThread.main(ActivityThread.java:4812) 
01-21 15:21:42.552: E/AndroidRuntime(16751): at java.lang.reflect.Method.invokeNative(Native Method) 
01-21 15:21:42.552: E/AndroidRuntime(16751): at java.lang.reflect.Method.invoke(Method.java:511) 
01-21 15:21:42.552: E/AndroidRuntime(16751): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
01-21 15:21:42.552: E/AndroidRuntime(16751): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 
01-21 15:21:42.552: E/AndroidRuntime(16751): at dalvik.system.NativeStart.main(Native Method) 
01-21 15:21:44.704: E/Trace(17053): error opening trace file: No such file or directory (2) 

請給您的寶貴時間和我非常感謝每一個機構。

+1

你需要'Context'在其他班級中顯示Toast。 – 2015-01-21 09:36:52

+1

getapplicationcontex() – 2015-01-21 09:37:41

回答

2

您需要Context才能在其他課程中顯示Toast

所以,你需要在你的信息創建一個參數的構造函數類像

private Context mCon; 
public void Messaging(Context con){ 
this.mCon=con; 
} 

然後用像

Toast.makeText(mCon, "Testing",Toast.LENGTH_LONG).show(); 

,並呼籲像

Messaging msg = new Messaging(getApplicationContext()); 
+0

謝謝你解決方案的工作...但我遇到一個新的問題,同時分配值來查看。 – 2015-01-21 09:57:17

+0

@Riturajsuman喲歡迎! – 2015-01-21 09:57:54

+0

呦。我正在用錯誤日誌更新我的問題,請檢查。 – 2015-01-21 09:59:01