2013-07-15 64 views
0

我一直在嘗試創建一個應用程序,該應用程序將圖像發送給另一個人,只有一個圖像。如果用戶想要手動輸入答案,應用程序的其中一個佈局具有文本字段,還有兩個按鈕:一個用於在聯繫人中搜索,以便用戶可以選擇聯繫人,另一個用於發送消息。發送按鈕應該確保在嘗試發送之前在文本字段中有輸入。我找到了一種發送文本的方式,比我找到的其他方式更簡單。我是Android開發新手。我在下面使用的方式是通過意圖。它還沒有添加圖片部分,因爲我想確保它首先工作,所以它只是發送你好世界。它似乎沒有在程序中的任何錯誤,但是當我按下發送按鈕時,出現一個錯誤,說應用程序不得不停止。你們知道我應該怎麼做才能解決這個問題嗎?我已經清理了幾次。感謝您的幫助。通過意圖發送短信導致應用程序停止

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.EditText; 

public class Send extends Activity implements OnClickListener { 
    EditText phoneNo = null; 
    String phoneNoString; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.send); 

     // Set up click listener for all buttons 
     View sendSmsButton = findViewById(R.id.send_sms_button); 
     sendSmsButton.setOnClickListener(this); 
     View contactsButton = findViewById(R.id.contacts); 
     contactsButton.setOnClickListener(this); 

     // TODO Auto-generated method stub 
    } 

    public void onClick(View v) { 
     switch (v.getId()) { 
     case R.id.send_sms_button: 
      phoneNoString = phoneNo.getEditableText().toString().trim(); 
      invokesSMSApp(); 
     } 
    } 

    public void invokesSMSApp() { 
     Intent smsIntent = new Intent(Intent.ACTION_VIEW); 

     smsIntent.putExtra("sms_body", "Hello World!"); 
     smsIntent.putExtra("address", phoneNoString); 
     smsIntent.setType("vnd.android-dir/mms-sms"); 

     startActivity(smsIntent); 
    } 
} 

這是我收到的錯誤:

07-15 00:56:09.213: E/AndroidRuntime(1098): FATAL EXCEPTION: main 
07-15 00:56:09.213: E/AndroidRuntime(1098): java.lang.NullPointerException 
07-15 00:56:09.213: E/AndroidRuntime(1098):  at com.example.getaroundtoit.Send.onClick(Send.java:31) 
07-15 00:56:09.213: E/AndroidRuntime(1098):  at android.view.View.performClick(View.java:4204) 
07-15 00:56:09.213: E/AndroidRuntime(1098):  at android.view.View$PerformClick.run(View.java:17355) 
07-15 00:56:09.213: E/AndroidRuntime(1098):  at android.os.Handler.handleCallback(Handler.java:725) 
07-15 00:56:09.213: E/AndroidRuntime(1098):  at android.os.Handler.dispatchMessage(Handler.java:92) 
07-15 00:56:09.213: E/AndroidRuntime(1098):  at android.os.Looper.loop(Looper.java:137) 
07-15 00:56:09.213: E/AndroidRuntime(1098):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
07-15 00:56:09.213: E/AndroidRuntime(1098):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-15 00:56:09.213: E/AndroidRuntime(1098):  at java.lang.reflect.Method.invoke(Method.java:511) 
07-15 00:56:09.213: E/AndroidRuntime(1098):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
07-15 00:56:09.213: E/AndroidRuntime(1098):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
07-15 00:56:09.213: E/AndroidRuntime(1098):  at dalvik.system.NativeStart.main(Native Method) 
07-15 00:56:16.835: E/Trace(1131): error opening trace file: No such file or directory (2) 
07-15 00:59:44.342: E/AndroidRuntime(1131): FATAL EXCEPTION: main 
07-15 00:59:44.342: E/AndroidRuntime(1131): java.lang.NullPointerException 
07-15 00:59:44.342: E/AndroidRuntime(1131):  at com.example.getaroundtoit.Send.onClick(Send.java:31) 
07-15 00:59:44.342: E/AndroidRuntime(1131):  at android.view.View.performClick(View.java:4204) 
07-15 00:59:44.342: E/AndroidRuntime(1131):  at android.view.View$PerformClick.run(View.java:17355) 
07-15 00:59:44.342: E/AndroidRuntime(1131):  at android.os.Handler.handleCallback(Handler.java:725) 
07-15 00:59:44.342: E/AndroidRuntime(1131):  at android.os.Handler.dispatchMessage(Handler.java:92) 
07-15 00:59:44.342: E/AndroidRuntime(1131):  at android.os.Looper.loop(Looper.java:137) 
07-15 00:59:44.342: E/AndroidRuntime(1131):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
07-15 00:59:44.342: E/AndroidRuntime(1131):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-15 00:59:44.342: E/AndroidRuntime(1131):  at java.lang.reflect.Method.invoke(Method.java:511) 
07-15 00:59:44.342: E/AndroidRuntime(1131):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
07-15 00:59:44.342: E/AndroidRuntime(1131):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
07-15 00:59:44.342: E/AndroidRuntime(1131):  at dalvik.system.NativeStart.main(Native Method) 

模擬器給出了模擬器的屏幕,上面寫着應用程序不得不停止錯誤消息。

+0

你得到什麼錯誤? – nedaRM

回答

1

你有這樣的:

EditText phoneNo = null; 

,然後你在呼喚:

phoneNoString = phoneNo.getEditableText().toString().trim(); 

但PHONENO爲null,並且這裏的問題。看來你缺少這樣的事情在onCreate方法:

phoneNo = (EditText)findViewById(<pnoneNo ID goes here>); 

希望這有助於...

+0

哦,謝謝!我完全錯過了! – lonelyminstrel

+0

@lonelyminstrel接受答案爲正確的答案將幫助我和他人看到這篇文章時。謝謝... – Christos