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().
Related
I'm trying unzip some files in background, so I use IntentService like in google's tutorial. My service class declared in AndroidManifest like this:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.osmdroid">
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<application
android:configChanges="orientation|screenSize|keyboardHidden"
android:hardwareAccelerated="true"
android:icon="#drawable/ecn_icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MapActivity"
android:icon="#drawable/ecn_icon"
android:label="test" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="#string/title_activity_settings">
</activity>
<service
android:name=".UnZipService"
android:exported="false"/>
</application>
In activity, I have
IntentFilter intentFilter = new IntentFilter(DownloadManager
.ACTION_DOWNLOAD_COMPLETE);
receiverDownloadComplete = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
long reference = intent.getLongExtra(DownloadManager
.EXTRA_DOWNLOAD_ID, -1);
if (myDownloadReference == reference) {
...
switch (status) {
case DownloadManager.STATUS_SUCCESSFUL:
Intent mServiceIntent = new Intent(context, UnZipService.class);
mServiceIntent.setData(Uri.parse(savedFilePath));
startActivity(mServiceIntent);
break;
...
}
cursor.close();
}
}
};
registerReceiver(receiverDownloadComplete, intentFilter);
And service here:
public class UnZipService extends IntentService {
public UnZipService() {
super("UnZipService");
}
#Override
protected void onHandleIntent(Intent workIntent) {
String dataString = workIntent.getDataString();
Log.v("IntentURI", dataString);
Toast.makeText(this, "Installing....", Toast.LENGTH_SHORT).show();
}
It should show toast just for test, but I always get an error:
01-29 19:08:25.740 15521-15521/org.osmdroid E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.DOWNLOAD_COMPLETE flg=0x10 pkg=org.osmdroid (has extras) } in org.osmdroid.SettingsActivity$1#21292650
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:768)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5147)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {org.osmdroid/org.osmdroid.UnZipService}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
at android.app.Activity.startActivityForResult(Activity.java:3404)
at android.app.Activity.startActivityForResult(Activity.java:3365)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:817)
at android.app.Activity.startActivity(Activity.java:3600)
at android.app.Activity.startActivity(Activity.java:3568)
at org.osmdroid.SettingsActivity$1.onReceive(SettingsActivity.java:148)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:758)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5147)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Both (class and activity) in same folder (org.osmdroid). Seems like paths is ok, but the problem appears and I have no more ideas...
You are starting a service as an activity
Change
Intent mServiceIntent = new Intent(context, UnZipService.class);
mServiceIntent.setData(Uri.parse(savedFilePath));
startActivity(mServiceIntent);
to
Intent mServiceIntent = new Intent(context, UnZipService.class);
mServiceIntent.setData(Uri.parse(savedFilePath));
startService(mServiceIntent); // Only this line is changed
Please declared this activity in your AndroidManifest.xml
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
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);
I try to implement an application based on rscm (middleware). When I try to run on emulator, I get the errors listed below:
Error :03-27 16:58:20.490: E/Trace(1508): error opening trace file: No
such file or directory (2)
03-27 21:53:21.610: D/AndroidRuntime(3803): Shutting down VM 03-27
21:53:21.610: W/dalvikvm(3803): threadid=1: thread exiting with
uncaught exception (group=0x40a71930) 03-27 21:53:21.760:
E/AndroidRuntime(3803): FATAL EXCEPTION: main 03-27 21:53:21.760:
E/AndroidRuntime(3803): java.lang.RuntimeException: Unable to
instantiate activity
ComponentInfo{com.example.context_application/com.example.context_application.MyContextAwareActivity}:
java.lang.ClassNotFoundException: Didn't find class
"com.example.context_application.MyContextAwareActivity" on path:
/data/app/com.example.context_application-2.apk 03-27 21:53:21.760:
E/AndroidRuntime(3803): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
03-27 21:53:21.760: E/AndroidRuntime(3803): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-27 21:53:21.760: E/AndroidRuntime(3803): at
android.app.ActivityThread.access$600(ActivityThread.java:141) 03-27
21:53:21.760: E/AndroidRuntime(3803): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-27 21:53:21.760: E/AndroidRuntime(3803): at
android.os.Handler.dispatchMessage(Handler.java:99) 03-27
21:53:21.760: E/AndroidRuntime(3803): at
android.os.Looper.loop(Looper.java:137) 03-27 21:53:21.760:
E/AndroidRuntime(3803): at
android.app.ActivityThread.main(ActivityThread.java:5041) 03-27
21:53:21.760: E/AndroidRuntime(3803): at
java.lang.reflect.Method.invokeNative(Native Method) 03-27
21:53:21.760: E/AndroidRuntime(3803): at
java.lang.reflect.Method.invoke(Method.java:511) 03-27 21:53:21.760:
E/AndroidRuntime(3803): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-27 21:53:21.760: E/AndroidRuntime(3803): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 03-27
21:53:21.760: E/AndroidRuntime(3803): at
dalvik.system.NativeStart.main(Native Method) 03-27 21:53:21.760:
E/AndroidRuntime(3803): Caused by: java.lang.ClassNotFoundException:
Didn't find class
"com.example.context_application.MyContextAwareActivity" on path:
/data/app/com.example.context_application-2.apk 03-27 21:53:21.760:
E/AndroidRuntime(3803): at
dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
03-27 21:53:21.760: E/AndroidRuntime(3803): at
java.lang.ClassLoader.loadClass(ClassLoader.java:501) 03-27
21:53:21.760: E/AndroidRuntime(3803): at
java.lang.ClassLoader.loadClass(ClassLoader.java:461) 03-27
21:53:21.760: E/AndroidRuntime(3803): at
android.app.Instrumentation.newActivity(Instrumentation.java:1054)
03-27 21:53:21.760: E/AndroidRuntime(3803): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
03-27 21:53:21.760: E/AndroidRuntime(3803): ... 11 more
Can anyone help?
package com.example.context;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import org.aspectsense.rscm.ContextValue;
import org.aspectsense.rscm.context.client.ContextListenerActivity;
import org.json.JSONException;
import java.util.Date;
public class MyContextAwareActivity extends ContextListenerActivity
{
#Override public String[] getRequestedScopes()
{
return new String[] { "battery.level" };
}
private TextView messageTextView;
#Override protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
messageTextView = new TextView(this);
setContentView(messageTextView);
appendMessage("Activity created");
}
private void appendMessage(final String message)
{
final String currentMessage = messageTextView.getText().toString();
messageTextView.setText(currentMessage + "\n" + message);
}
#Override public void onContextValueChanged(ContextValue contextValue)
{
try
{
appendMessage(new Date() + ": The battery level is " + contextValue.getValueAsInteger() + "%");
}
catch (JSONException jsone)
{
Toast.makeText(this, "Error while displaying context event: " + contextValue, Toast.LENGTH_SHORT).show();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.context"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.context.MyContextAwareActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You are telling it to write to external storage. Have you set up an SD card in eclipse?
Do any of these answers help? error opening trace file: No such file or directory (2)
I am trying to establish a bluetooth connection between my phone and a bluetooth device, but the app keeps crashing. By commenting, i have found out that the error is in the openBT() function. Can anyone help me out please?
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class PageOne extends Activity {
TextView myLabel;
TextView deviceFound;
Button openButton,closeButton;
BluetoothAdapter mBluetoothAdapter;
BluetoothDevice mmDevice;
BluetoothSocket mmSocket;
OutputStream mmOutputStream;
InputStream mmInputStream;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pageone);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
setUp();
openButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (openButton.getText().equals("Enable")) {
findBT();
}
if(openButton.getText().equals("Start Connection")){
System.out.println("here");
try{
openBT();
}catch (IOException e){e.printStackTrace();};
}
}
});
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
closeBT();
System.out.println("here too");
}
});
}
private void setUp() {
openButton = (Button) findViewById(R.id.button1);
myLabel = (TextView) findViewById(R.id.textView1);
closeButton = (Button) findViewById(R.id.button2);
deviceFound = (TextView) findViewById(R.id.textView2);
setButtonText();
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
setButtonText();
}
};
IntentFilter filter = new IntentFilter (BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver (receiver, filter);
}
private void setButtonText() {
closeButton.setText("Disable bluetooth");
if (mBluetoothAdapter.isEnabled()) {
openButton.setText("Start Connection");
myLabel.setText("Bluetooth is enabled");
} else {
openButton.setText("Enable");
myLabel.setText("Bluetooth is disabled");
}
}
private void findBT(){
if(mBluetoothAdapter == null)
{
myLabel.setText("No bluetooth adapter available");
}
if (!mBluetoothAdapter.isEnabled()) {
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0)
{
for(BluetoothDevice device : pairedDevices)
{
if(device.getName().equals("Hauwa"))
{
mmDevice = device;
break;
}
}
}
deviceFound.setText("Bluetooth Device Found");
}
private void closeBT(){
if (mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.disable();
deviceFound.setText("bvnbvnvb");
}
}
void openBT() throws IOException{
final UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();
}
}
and here is the logcat error
01-07 02:55:23.189: W/dalvikvm(11382): threadid=1: thread exiting with uncaught exception (group=0x401f0560)
01-07 02:55:23.189: E/AndroidRuntime(11382): FATAL EXCEPTION: main
01-07 02:55:23.189: E/AndroidRuntime(11382): java.lang.NullPointerException
01-07 02:55:23.189: E/AndroidRuntime(11382): at com.example.BluetoothExample.PageOne.openBT(PageOne.java:147)
01-07 02:55:23.189: E/AndroidRuntime(11382): at com.example.BluetoothExample.PageOne$1.onClick(PageOne.java:51)
01-07 02:55:23.189: E/AndroidRuntime(11382): at android.view.View.performClick(View.java:2579)
01-07 02:55:23.189: E/AndroidRuntime(11382): at android.view.View$PerformClick.run(View.java:9246)
01-07 02:55:23.189: E/AndroidRuntime(11382): at android.os.Handler.handleCallback(Handler.java:587)
01-07 02:55:23.189: E/AndroidRuntime(11382): at android.os.Handler.dispatchMessage(Handler.java:92)
01-07 02:55:23.189: E/AndroidRuntime(11382): at android.os.Looper.loop(Looper.java:130)
01-07 02:55:23.189: E/AndroidRuntime(11382): at android.app.ActivityThread.main(ActivityThread.java:3735)
01-07 02:55:23.189: E/AndroidRuntime(11382): at java.lang.reflect.Method.invokeNative(Native Method)
01-07 02:55:23.189: E/AndroidRuntime(11382): at java.lang.reflect.Method.invoke(Method.java:507)
01-07 02:55:23.189: E/AndroidRuntime(11382): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
01-07 02:55:23.189: E/AndroidRuntime(11382): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662)
01-07 02:55:23.189: E/AndroidRuntime(11382): at dalvik.system.NativeStart.main(Native Method)
01-07 02:55:23.199: E/AndroidRuntime(11382): [Blue Error Handler] Make Debugging Report file for main
01-07 02:55:23.199: E/AndroidRuntime(11382): java.lang.NullPointerException
01-07 02:55:23.199: E/AndroidRuntime(11382): at com.example.BluetoothExample.PageOne.openBT(PageOne.java:147)
01-07 02:55:23.199: E/AndroidRuntime(11382): at com.example.BluetoothExample.PageOne$1.onClick(PageOne.java:51)
01-07 02:55:23.199: E/AndroidRuntime(11382): at android.view.View.performClick(View.java:2579)
01-07 02:55:23.199: E/AndroidRuntime(11382): at android.view.View$PerformClick.run(View.java:9246)
01-07 02:55:23.199: E/AndroidRuntime(11382): at android.os.Handler.handleCallback(Handler.java:587)
01-07 02:55:23.199: E/AndroidRuntime(11382): at android.os.Handler.dispatchMessage(Handler.java:92)
01-07 02:55:23.199: E/AndroidRuntime(11382): at android.os.Looper.loop(Looper.java:130)
01-07 02:55:23.199: E/AndroidRuntime(11382): at android.app.ActivityThread.main(ActivityThread.java:3735)
01-07 02:55:23.199: E/AndroidRuntime(11382): at java.lang.reflect.Method.invokeNative(Native Method)
01-07 02:55:23.199: E/AndroidRuntime(11382): at java.lang.reflect.Method.invoke(Method.java:507)
01-07 02:55:23.199: E/AndroidRuntime(11382): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
01-07 02:55:23.199: E/AndroidRuntime(11382): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662)
01-07 02:55:23.199: E/AndroidRuntime(11382): at dalvik.system.NativeStart.main(Native Method)
are you sure that you have mmDevice not null? Are you sure you set mmDevice in findBT()?
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0)
{
for(BluetoothDevice device : pairedDevices)
{
if(device.getName().equals("Hauwa"))
{
mmDevice = device;
break;
}
}
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
String tmpS;
String targetS = "Hauwa";
if(pairedDevices.size() > 0)
{
for(BluetoothDevice device : pairedDevices)
{
tmpS = device.getName() + " ";
tmpS = tmpS.substring(0,targetS.length());
if(tmpS.equals(targetS))
{
mmDevice = device;
break;
}
}
you need to prevent the device name to be null. Even if your own device has name that is not null, the code could read other BLE device nearby that is null. You also need to prevent overflow at substring and equals functions,