我刚接触Android工作室,我遇到了这个问题。我无法获取我的EditText字符串并将其作为短信发送。当我使用EditText时应用程序崩溃,但当我使用固定的单词时,它运行得非常好。
这是我的MainActivity:
public class MainActivity extends AppCompatActivity implements communicator{
..
....
..
EditText t1,t2,t3,t4;
String phoneNo="09857512187",sms;
..
...
...
@Override
protected void onCreate(Bundle savedInstanceState){
...
...
...
...
ttl=(TextView) findViewById(R.id.total);
done=(Button) findViewById(R.id.done);
t1=(EditText)findViewById(R.id.first_name);
t2=(EditText)findViewById(R.id.last_name);
t3=(EditText)findViewById(R.id.numberInput);
t4=(EditText)findViewById(R.id.address);
sms=t1.getText().toString()+t2.getText().toString()+t3.getText().toString()+t4.getText().toString();
}
public void done(View v){
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
}
并且继承了logcat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jackreynaldo.tab/com.example.jackreynaldo.tab.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5357)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:952)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.jackreynaldo.tab.MainActivity.onCreate(MainActivity.java:109)
at android.app.Activity.performCreate(Activity.java:6012)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5357)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:952)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
注意:完成是我的onClick for my button
答案 0 :(得分:0)
根据LogCat,您的某个edittext未正确初始化。确保引用正确的资源ID。