我在我的项目中使用了一个底栏,其中有一个邮件选项。其中有附件选项。我的问题是当我单击附件按钮时,数据写入到编辑文本中,主题 n 消息被删除。选择附件文件后,我如何访问该编辑文本。为此 m 使用全局类。但我得到空对象引用。
public class MainActivity extends AppCompatActivity {
BottomBar nBottomBar;
EditText et1;
EditText et2;
EditText to;
EditText subject;
EditText message;
Uri u;
public static final String DEFAULT = "N/A";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
to= (EditText) findViewById(R.id.to);
subject = (EditText) findViewById(R.id.subject);
message= (EditText) findViewById(R.id.message);
nBottomBar = BottomBar.attach(this, savedInstanceState);
nBottomBar.setItemsFromMenu(R.menu.menu_main, new OnMenuTabClickListener() {
@Override
public void onMenuTabSelected(int i) {
if (i == R.id.bottombaritemone) {
PeopleFragment f = new PeopleFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.frame, f).commit();
} else if (i == R.id.bottombaritemtwo) {
CardFragment f = new CardFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.frame, f).commit();
} else if (i == R.id.bottombaritemthree) {
PhoneFragment f = new PhoneFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.frame, f).commit();
} else if (i == R.id.bottombaritemfour) {
MailFragment f = new MailFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.frame, f).commit();
} else if (i == R.id.bottombaritemfive) {
FavouriteFragment f = new FavouriteFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.frame, f).commit();
}
}
@Override
public void onMenuTabReSelected(int i) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void sendSMS(View v) {
et1 = (EditText) findViewById(R.id.phone);
et2 = (EditText) findViewById(R.id.msg);
SmsManager sManager = SmsManager.getDefault();
StringTokenizer str = new StringTokenizer(et1.getText().toString(), ",");
while (str.hasMoreElements()) {
String num = str.nextToken();
sManager.sendTextMessage(num, null, et2.getText().toString(), null, null);
}
}
public void call(View v) {
et1 = (EditText) findViewById(R.id.phone);
Intent i = new Intent();
i.setAction(Intent.ACTION_CALL);
i.setData(Uri.parse("tel:" + et1.getText().toString()));
startActivity(i);
}
public void attachment(View v) {
ApplicationConstants.to=to.getText().toString();
ApplicationConstants.subject=subject.getText().toString();
ApplicationConstants.message=message.getText().toString();
Intent i = new Intent();
i.setAction(Intent.ACTION_GET_CONTENT);
i.setType("*/*");
startActivityForResult(i, 123);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
to.setText(ApplicationConstants.to);
subject.setText(ApplicationConstants.subject);
message.setText(ApplicationConstants.message);
Toast.makeText(this, "Data loaded successfully", Toast.LENGTH_LONG).show();
u = data.getData();
}
public void sendMail(View v) {
Intent i = new Intent();
i.setAction(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_EMAIL, new String[]{to.getText().toString()});
i.putExtra(Intent.EXTRA_SUBJECT, subject.getText().toString());
i.putExtra(Intent.EXTRA_TEXT, message.getText().toString());
i.putExtra(Intent.EXTRA_STREAM, u);
i.setType("message/rfc822");
startActivity(i.createChooser(i, "select any Email client "));
}
}
日志猫:-
FATAL EXCEPTION: main
Process: com.example.hope.bottombar, PID: 24938
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5207)
at android.view.View$PerformClick.run(View.java:21168)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5207)
at android.view.View$PerformClick.run(View.java:21168)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
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.hope.bottombar.MainActivity.sendMail(MainActivity.java:145)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5207)
at android.view.View$PerformClick.run(View.java:21168)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)