NullPointerException when using EditText - java

I have the following function that i call from 2 different functions. the first way i call all is correct, the second way i get the following NullPointerException.
any idea why ?
private void doSearch(View v) {
Activity activity = getActivity();
//get the text:
EditText editSearch = (EditText) v.findViewById(R.id.editSearch);
String query = editSearch.getText().toString();
//check if 2 letters were entered for search
if (query.length()>1){
//call service with extra
Intent intent = new Intent(activity, SearchPlacesService.class);
intent.putExtra("query", query);
activity.startService(intent);
}else{
Toast.makeText(getActivity(), "Please enter 2 letters for search", Toast.LENGTH_SHORT).show();
}
}
//good call
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
switch (v.getId()) {
case R.id.editSearch:
doSearch(v);
return true;
}
return false;
}
//unsuccessful call
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSearch:
doSearch((TextView) v);
break;
}
}
the Logcat:
01-07 09:29:08.843: E/AndroidRuntime(1078): FATAL EXCEPTION: main
01-07 09:29:08.843: E/AndroidRuntime(1078): java.lang.NullPointerException
01-07 09:29:08.843: E/AndroidRuntime(1078): at com.lora_solomon.myfavoriteplaces.view.FragmentList.doSearch(FragmentList.java:137)
01-07 09:29:08.843: E/AndroidRuntime(1078): at com.lora_solomon.myfavoriteplaces.view.FragmentList.onClick(FragmentList.java:100)
01-07 09:29:08.843: E/AndroidRuntime(1078): at android.view.View.performClick(View.java:4084)
01-07 09:29:08.843: E/AndroidRuntime(1078): at android.view.View$PerformClick.run(View.java:16966)
01-07 09:29:08.843: E/AndroidRuntime(1078): at android.os.Handler.handleCallback(Handler.java:615)
01-07 09:29:08.843: E/AndroidRuntime(1078): at android.os.Handler.dispatchMessage(Handler.java:92)
01-07 09:29:08.843: E/AndroidRuntime(1078): at android.os.Looper.loop(Looper.java:137)
01-07 09:29:08.843: E/AndroidRuntime(1078): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-07 09:29:08.843: E/AndroidRuntime(1078): at java.lang.reflect.Method.invokeNative(Native Method)
01-07 09:29:08.843: E/AndroidRuntime(1078): at java.lang.reflect.Method.invoke(Method.java:511)
01-07 09:29:08.843: E/AndroidRuntime(1078): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-07 09:29:08.843: E/AndroidRuntime(1078): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-07 09:29:08.843: E/AndroidRuntime(1078): at dalvik.system.NativeStart.main(Native Method)
appreciate any help!!

Here you are passing TextView as an arguement for the doSearch method. And you are getting the EditText by using TextView so obviously editText is null
I guess this line is causing NPE
String query = editSearch.getText().toString();
Try this..for fetching the EditText
View parent =(View) v.getParent();
EditText editSearch = (EditText) parent.findViewById(R.id.editSearch);

you are casting the view in your function to an EditText and you are passing TextView in OnClick try this doSearch((EditText)v);

//unsuccessful call
public void onClick(View v)
{
switch (v.getId()) {
case R.id.btnSearch:
doSearch((TextView) v);//changes
break;
}
}
In your above code you are passing view as Textview doSearch((TextView)v) and in your doSearch method implementation inner you are casting TextView to EditText this reason arrising NullpointerException( EditText editSearch = (EditText) v.findViewById(R.id.editSearch);)
Changes
doSearch((TextView) v);
to
doSearch((EditText) v);

Related

Checkbox in the popupMenu

I have popupMenu and CheckBox. I need make write status CheckBox to boolean.
This code not working:
MenuItem fast_result;
boolean fast=false;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.FastResult:
fast_result = item.getSubMenu().getItem(R.id.FastResult);//This is 182 line
fast_result.setChecked(!fast_result.isChecked());
fast=fast_result.isChecked();
return true;
}
}
It is errors:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.alexvsalex.HelpforMath.RootsActivity.onOptionsItemSelected(RootsActivity.java:182)
at android.app.Activity.onMenuItemSelected(Activity.java:2502)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:950)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:163)
at android.widget.AdapterView.performItemClick(AdapterView.java:292)
at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
at android.widget.AbsListView$1.run(AbsListView.java:3168)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
What to do?
The problem is solved:
case R.id.FastResult:
fast_result = item; //There was an error
fast_result.setChecked(!fast_result.isChecked());
fast=fast_result.isChecked();
return true;

List shows as not null but ArrayAdapter throws NullPointerException

Can anyone please help me? I just don't understand why does it throw null pointer exception.
I work with ksoap2,WCF web methods and android. I checked the "list" in debugger mode and it is not null.
Here is my code:
public class MainActivity extends Activity {
String loginMemberId = "a001";
Context context;
List list;
ListView listView;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getBeneficiariesNameSet BNL = new getBeneficiariesNameSet();
try {
list = BNL.execute().get();
if(list!=null && list.size()>0) {
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, list); //null pointer exception,line 43
listView = (ListView) findViewById(R.id.listBeneficiaries);
listView.setAdapter(arrayAdapter);
}else{
Toast.makeText(context,"No data in array",Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public class getBeneficiariesNameSet extends AsyncTask<String, Void, List<String>> {
private final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
#Override
protected List<String> doInBackground(String... params) {
TestArray CS = new TestArray();
List<String> response = CS.getBeneficiariesNameSet(loginMemberId);
return response;
}
#Override
protected void onPostExecute(List<String> s) {
super.onPostExecute(s);
dialog.dismiss();
}
}
}
And here is the Log cat.
06-11 14:22:20.484 22245-22245/? W/System.err﹕ java.lang.NullPointerException
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at com.example.administrator.androidwcf.MainActivity$1.onClick(MainActivity.java:43)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at android.view.View.performClick(View.java:4443)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at android.view.View$PerformClick.run(View.java:18442)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5021)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
06-11 14:22:20.484 22245-22245/? W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
06-11 14:22:20.514 599-624/? W/audio_hw_primary﹕ out_write() limiting sleep time 112539 to 46439
06-11 14:22:20.584 599-624/? W/audio_hw_primary﹕ out_write() limiting sleep time 65759 to 46439
Your context is null. Use MainActivity.this for a context in an anon inner class of your activity.

My Android Application to show text on Click Stop Working

I have a very very simple project in which what I am trying to achieve is to that when a user click on the button it display the text. But in the ADB it says unfortunately showtext stop working. What is the reason behind this?
I had tried but not got any result. My code is fine but not working. Below is java code for it. I omitted xml code as it contain only the button and a text view not so complex.
public class MainActivity extends Activity {
Button ShowText;
TextView DisplayText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ShowText=(Button)findViewById(R.id.bShowText);
}
void ShowMeText(View view){
DisplayText=(TextView)findViewById(R.id.tvShowText);
DisplayText.setVisibility(View.VISIBLE);
}
}
Try solve my this little issue. Thanks in Advance.
As someone asked for logcat error here is it -
06-11 06:51:32.376: E/AndroidRuntime(1223): FATAL EXCEPTION: main
06-11 06:51:32.376: E/AndroidRuntime(1223): java.lang.IllegalStateException: Could not find a method ShowMeText(View) in the activity class com.mylearning.showtext.MainActivity for onClick handler on view class android.widget.Button with id 'bShowText'
06-11 06:51:32.376: E/AndroidRuntime(1223): at android.view.View$1.onClick(View.java:3620)
06-11 06:51:32.376: E/AndroidRuntime(1223): at android.view.View.performClick(View.java:4240)
06-11 06:51:32.376: E/AndroidRuntime(1223): at android.view.View$PerformClick.run(View.java:17721)
06-11 06:51:32.376: E/AndroidRuntime(1223): at android.os.Handler.handleCallback(Handler.java:730)
06-11 06:51:32.376: E/AndroidRuntime(1223): at android.os.Handler.dispatchMessage(Handler.java:92)
06-11 06:51:32.376: E/AndroidRuntime(1223): at android.os.Looper.loop(Looper.java:137)
06-11 06:51:32.376: E/AndroidRuntime(1223): at android.app.ActivityThread.main(ActivityThread.java:5103)
06-11 06:51:32.376: E/AndroidRuntime(1223): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 06:51:32.376: E/AndroidRuntime(1223): at java.lang.reflect.Method.invoke(Method.java:525)
06-11 06:51:32.376: E/AndroidRuntime(1223): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-11 06:51:32.376: E/AndroidRuntime(1223): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-11 06:51:32.376: E/AndroidRuntime(1223): at dalvik.system.NativeStart.main(Native Method)
06-11 06:51:32.376: E/AndroidRuntime(1223): Caused by: java.lang.NoSuchMethodException: ShowMeText [class android.view.View]
06-11 06:51:32.376: E/AndroidRuntime(1223): at java.lang.Class.getConstructorOrMethod(Class.java:423)
06-11 06:51:32.376: E/AndroidRuntime(1223): at java.lang.Class.getMethod(Class.java:787)
06-11 06:51:32.376: E/AndroidRuntime(1223): at android.view.View$1.onClick(View.java:3613)
06-11 06:51:32.376: E/AndroidRuntime(1223): ... 11 more
You need to add android:onClick="ShowMeText" in your Button in your Layout. like so
<Button .............
android:onClick="ShowMeText"
......... />
and used lower case for method creation as Java naming Convention . like so showmetext
and also defined your ShowMeText(...) method as Public
ShowMeText(View v) has to be public.
Btw. start function names with lower case characters as specified by Java code style.
<Button
android:id="#+id/tvShowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="ShowMeText" />
in java code
public void ShowMeText(View v){
DisplayText=(TextView)findViewById(R.id.tvShowText);
DisplayText.setVisibility(View.VISIBLE);
}
public class MainActivity extends Activity implements OnClickListener {
Button ShowText;
TextView DisplayText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ShowText = (Button) findViewById(R.id.bShowText);
DisplayText = (TextView) findViewById(R.id.tvShowText);
ShowText.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v.getId() == ShowText.getId())
// need to check ID because from which view has fire event soo we can handle appropriate
DisplayText.setVisibility(View.VISIBLE);
}
}
// Try this way,hope this will help you to solve your problem.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="#+id/bShowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ShowText"
android:onClick="ShowMeText"/>
<TextView
android:id="#+id/tvShowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:visibility="gone"
android:text="TextView With Some Text"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private TextView tvShowText;
private Button bShowText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvShowText = (TextView)findViewById(R.id.tvShowText);
bShowText = (Button)findViewById(R.id.bShowText);
}
public void ShowMeText(View v){
if(v.getId() == bShowText.getId()) {
if(tvShowText.getVisibility() == View.VISIBLE){
tvShowText.setVisibility(View.GONE);
}else{
tvShowText.setVisibility(View.VISIBLE);
}
}
}
}
Try this:
public class MainActivity extends Activity {
Button ShowText;
TextView DisplayText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ShowText=(Button)findViewById(R.id.bShowText);
DisplayText=(TextView)findViewById(R.id.tvShowText);
DisplayText.setVisibility(View.Gone);
ShowText.setonClickListener(this);
}
public void ShowMeText(View view){
DisplayText.setVisibility(View.VISIBLE);
DisplayText.setText("Show your Text");
}
}

LibGDX: Activity has leaked window when trying to post to Facebook

Context: I created a LibGDX game (extends AndroidApplication) that switches to another activity (extends FragmentActivity) to start a social Facebook sharing activity (extends Fragments). I followed the Tutorial here: https://developers.facebook.com/docs/android/scrumptious/publish-open-graph-story#step6c
However, when I try to run my program, I get a Activity leaked window error. Full error log below:
01-07 00:20:33.160: E/WindowManager(23786): Activity com.pressx.thedevice.SocialActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView#41cca0c0 that was originally added here
01-07 00:20:33.160: E/WindowManager(23786): android.view.WindowLeaked: Activity com.pressx.thedevice.SocialActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView#41cca0c0 that was originally added here
01-07 00:20:33.160: E/WindowManager(23786): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
01-07 00:20:33.160: E/WindowManager(23786): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:307)
01-07 00:20:33.160: E/WindowManager(23786): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:239)
01-07 00:20:33.160: E/WindowManager(23786): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:152)
01-07 00:20:33.160: E/WindowManager(23786): at android.view.Window$LocalWindowManager.addView(Window.java:547)
01-07 00:20:33.160: E/WindowManager(23786): at android.app.Dialog.show(Dialog.java:282)
01-07 00:20:33.160: E/WindowManager(23786): at android.app.ProgressDialog.show(ProgressDialog.java:116)
01-07 00:20:33.160: E/WindowManager(23786): at android.app.ProgressDialog.show(ProgressDialog.java:99)
01-07 00:20:33.160: E/WindowManager(23786): at com.pressx.facebook.SelectionFragment.handleAnnounce(SelectionFragment.java:492)
01-07 00:20:33.160: E/WindowManager(23786): at com.pressx.facebook.SelectionFragment.access$2(SelectionFragment.java:480)
01-07 00:20:33.160: E/WindowManager(23786): at com.pressx.facebook.SelectionFragment$2.onClick(SelectionFragment.java:385)
01-07 00:20:33.160: E/WindowManager(23786): at android.view.View.performClick(View.java:4101)
01-07 00:20:33.160: E/WindowManager(23786): at android.view.View$PerformClick.run(View.java:17087)
01-07 00:20:33.160: E/WindowManager(23786): at android.os.Handler.handleCallback(Handler.java:615)
01-07 00:20:33.160: E/WindowManager(23786): at android.os.Handler.dispatchMessage(Handler.java:92)
01-07 00:20:33.160: E/WindowManager(23786): at android.os.Looper.loop(Looper.java:137)
01-07 00:20:33.160: E/WindowManager(23786): at android.app.ActivityThread.main(ActivityThread.java:4849)
01-07 00:20:33.160: E/WindowManager(23786): at java.lang.reflect.Method.invokeNative(Native Method)
01-07 00:20:33.160: E/WindowManager(23786): at java.lang.reflect.Method.invoke(Method.java:511)
01-07 00:20:33.160: E/WindowManager(23786): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
01-07 00:20:33.160: E/WindowManager(23786): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
01-07 00:20:33.160: E/WindowManager(23786): at dalvik.system.NativeStart.main(Native Method)
Reading around got me that maybe a ProgressDialog issue occurred. However, I followed the Facebook tutorial and ran the sample app that it created, no such error occurred.
Following are all locations of ProgressDialog and their contexts:
Private Variable
private ProgressDialog progressDialog;
in
public class SelectionFragment extends Fragment {
...
private ProgressDialog progressDialog;
...
}
ProgressDialog.show()
progressDialog = ProgressDialog.show(getActivity(), "", getActivity().getResources().getString(R.string.progress_dialog_text), true);
in
private void handleAnnounce() {
pendingAnnounce = false;
Session session = Session.getActiveSession();
if(session == null || !session.isOpened())
return;
List<String> permissions = session.getPermissions();
if(!permissions.containsAll(PERMISSIONS)) {
pendingAnnounce = true;
requestPublishPermissions(session);
return;
}
progressDialog = ProgressDialog.show(getActivity(), "", getActivity().getResources().getString(R.string.progress_dialog_text), true);
AsyncTask<Void, Void, Response> task = new AsyncTask<Void, Void, Response>() {
#Override
protected Response doInBackground(Void... voids) {
HitAction hitAction = GraphObject.Factory.create(HitAction.class);
for(BaseListElement element : listElements) {
element.populateOGAction(hitAction);
}
Request request = new Request(Session.getActiveSession(), POST_ACTION_PATH, null, HttpMethod.POST);
request.setGraphObject(hitAction);
return request.executeAndWait();
}
#Override
protected void onPostExecute(Response response) {
onPostActionResponse(response);
}
};
task.execute();
}
if() condition
if(progressDialog != null) {
progressDialog.dismiss();
progressDialog = null;
}
in
private void onPostActionResponse(Response response) {
if(progressDialog != null) {
progressDialog.dismiss();
progressDialog = null;
}
if(getActivity() == null)
return;
PostResponse postResponse = response.getGraphObjectAs(PostResponse.class);
if(postResponse != null && postResponse.getId() != null) {
String dialogBody = String.format(getString(R.string.result_dialog_text), postResponse.getId());
new AlertDialog.Builder(getActivity()).setPositiveButton(R.string.result_dialog_button_text, null).setTitle(R.string.result_dialog_title).setMessage(dialogBody).show();
init(null);
}
else
handleError(response.getError());
}
Full Code Here: https://github.com/putty174/TheDevice/blob/Max/TheDevice-android/src/com/pressx/facebook/SelectionFragment.java

send email activity in new instance

I'm trying to send an email from my email class but when the program gets to startActivity it crashes I think it might has something to do with the manifest. Below is my main activity
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class InvoiceActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void SendMessage(View v)
{
// get email parameters
SMTPmail mail = new SMTPmail();
mail.SendSMTP("body of email","subject of email","recipient#example.com");
}
}
here is the SMTPmail
import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;
public class SMTPmail extends Activity {
public void SendSMTP(String message, String subject, String recipted)
{
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{recipted});
i.putExtra(Intent.EXTRA_CC, "");
i.putExtra(Intent.EXTRA_SUBJECT, subject);
i.putExtra(Intent.EXTRA_TEXT , message);
try {
startActivity(Intent.createChooser(i, "Send mail..."));//crashes here in debug
finish();
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(SMTPmail.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
}
this is the manifest
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".InvoiceActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SMTPmail"
android:noHistory="true">
</activity>
</application>
logcat below
01-07 00:01:25.199: D/dalvikvm(14198): GC_EXTERNAL_ALLOC freed 43K, 51% free 2687K/5379K, external 0K/0K, paused 54ms
01-07 00:01:30.389: D/AndroidRuntime(14198): Shutting down VM
01-07 00:01:30.389: W/dalvikvm(14198): threadid=1: thread exiting with uncaught exception (group=0x40018560)
01-07 00:01:30.399: E/AndroidRuntime(14198): FATAL EXCEPTION: main
01-07 00:01:30.399: E/AndroidRuntime(14198): java.lang.IllegalStateException: Could not execute method of the activity
01-07 00:01:30.399: E/AndroidRuntime(14198): at android.view.View$1.onClick(View.java:2165)
01-07 00:01:30.399: E/AndroidRuntime(14198): at android.view.View.performClick(View.java:2506)
01-07 00:01:30.399: E/AndroidRuntime(14198): at android.view.View$PerformClick.run(View.java:9112)
01-07 00:01:30.399: E/AndroidRuntime(14198): at android.os.Handler.handleCallback(Handler.java:587)
01-07 00:01:30.399: E/AndroidRuntime(14198): at android.os.Handler.dispatchMessage(Handler.java:92)
01-07 00:01:30.399: E/AndroidRuntime(14198): at android.os.Looper.loop(Looper.java:130)
01-07 00:01:30.399: E/AndroidRuntime(14198): at android.app.ActivityThread.main(ActivityThread.java:3835)
01-07 00:01:30.399: E/AndroidRuntime(14198): at java.lang.reflect.Method.invokeNative(Native Method)
01-07 00:01:30.399: E/AndroidRuntime(14198): at java.lang.reflect.Method.invoke(Method.java:507)
01-07 00:01:30.399: E/AndroidRuntime(14198): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
01-07 00:01:30.399: E/AndroidRuntime(14198): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
01-07 00:01:30.399: E/AndroidRuntime(14198): at dalvik.system.NativeStart.main(Native Method)
01-07 00:01:30.399: E/AndroidRuntime(14198): Caused by: java.lang.reflect.InvocationTargetException
01-07 00:01:30.399: E/AndroidRuntime(14198): at java.lang.reflect.Method.invokeNative(Native Method)
01-07 00:01:30.399: E/AndroidRuntime(14198): at java.lang.reflect.Method.invoke(Method.java:507)
01-07 00:01:30.399: E/AndroidRuntime(14198): at android.view.View$1.onClick(View.java:2160)
01-07 00:01:30.399: E/AndroidRuntime(14198): ... 11 more
01-07 00:01:30.399: E/AndroidRuntime(14198): Caused by: java.lang.NullPointerException
01-07 00:01:30.399: E/AndroidRuntime(14198): at android.app.Activity.startActivityForResult(Activity.java:2827)
01-07 00:01:30.399: E/AndroidRuntime(14198): at android.app.Activity.startActivity(Activity.java:2933)
01-07 00:01:30.399: E/AndroidRuntime(14198): at com.android.EUROPE.Invoice.SMTPmail.SendSMTP(SMTPmail.java:17)
01-07 00:01:30.399: E/AndroidRuntime(14198): at com.android.EUROPE.Invoice.InvoiceActivity.SendMessage(InvoiceActivity.java:19)
01-07 00:01:30.399: E/AndroidRuntime(14198): ... 14 more
As James already said, there is no reason for SMTPmail to extend Activity. A simple implementation could look like this.
SMTPmail:
public class SMTPmail {
public static void sendSMTP(Context context, String message, String subject, String recipted)
{
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{recipted});
i.putExtra(Intent.EXTRA_CC, "");
i.putExtra(Intent.EXTRA_SUBJECT, subject);
i.putExtra(Intent.EXTRA_TEXT , message);
try {
context.startActivity(Intent.createChooser(i, "Send mail..."));//crashes here in debug
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(context, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
}
In InvoiceActivity:
public void SendMessage(View v)
{
SMTPmail.sendSMTP(this, "body of email","subject of email","recipient#example.com");
}
Is there a reason SMTPmail extends Activity? This looks superfluous. Just change STMPmail so that it does not extend any classes and this should work. You will have to pass a Context into that method to get things like startActivity().

Categories