I have button which function is to take screenshot and share it to Facebook. when i try it in my Samsung with JellyBean OS device its working perfectly, but when i try it to BlueStack with JellyBean OS and CheeryMobile with Lollipop OS things are not working, i check the Logcat and i see some error.
my code
onClick of Button call takeScreenshot();
private void takeScreenshot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
sharePhotoToFacebook(imageFile);
} catch (Throwable
e.printStackTrace();
}
}
private void sharePhotoToFacebook(File imageFile){
Uri imageUri = Uri.fromFile(imageFile);
try {
image = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
} catch (IOException e) {
e.printStackTrace();
}
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
List<String> permissionNeeds = Arrays.asList("publish_actions");
//this loginManager helps you eliminate adding a LoginButton to your UI
loginManager = LoginManager.getInstance();
loginManager.logInWithPublishPermissions(this, permissionNeeds);
loginManager.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
try{
SharePhoto photo = new SharePhoto.Builder()
.setBitmap(image)
.setCaption(scoreFinal)
.build();
SharePhotoContent content = new SharePhotoContent.Builder()
.addPhoto(photo)
.build();
ShareApi.share(content, null);
final Dialog dialog = new Dialog(ActivityShare.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.sucess_shared);
dialog.show();
Button btnOk = (Button) dialog.findViewById(R.id.btnOk);
btnOk.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
}catch (Exception e){
Log.e("Error on share", String.valueOf(e));
}
}
#Override
public void onCancel() {
Log.w("OnCancel", "Canceled by user");
// System.out.println("onCancel");
}
#Override
public void onError(FacebookException exception) {
final Dialog dialog = new Dialog(ActivityShare.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.sucess_shared);
TextView title = (TextView)dialog.findViewById(R.id.titleSuccess);
TextView desc = (TextView)dialog.findViewById(R.id.descriptionHere);
title.setText("Unsuccessfull");
desc.setText("Something's wrong, Please check your internet connection and try again.");
dialog.show();
Button btnOk = (Button) dialog.findViewById(R.id.btnOk);
btnOk.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
Log.e("OnError", String.valueOf(exception));
// System.out.println("onError");
}
});
}
LogCat Error
01-07 23:34:36.767 2024-2024/com.sample.app W/System.err: java.io.FileNotFoundException: /mnt/sdcard/Thu Jan 07 23:34:36 SGT 2016.jpg: open failed: EINVAL (Invalid argument)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at libcore.io.IoBridge.open(IoBridge.java:406)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at com.sample.app.ActivityShare.takeScreenshot(ActivityShare.java:225)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at com.sample.app.ActivityShare.access$000(ActivityShare.java:42)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at com.sample.app.ActivityShare$1.onClick(ActivityShare.java:103)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at android.view.View.performClick(View.java:3511)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at android.view.View$PerformClick.run(View.java:14105)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at android.os.Handler.handleCallback(Handler.java:605)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at android.os.Handler.dispatchMessage(Handler.java:92)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at android.os.Looper.loop(Looper.java:137)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at android.app.ActivityThread.main(ActivityThread.java:4424)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at java.lang.reflect.Method.invoke(Method.java:511)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at dalvik.system.NativeStart.main(Native Method)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: Caused by: libcore.io.ErrnoException: open failed: EINVAL (Invalid argument)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at libcore.io.Posix.open(Native Method)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: at libcore.io.IoBridge.open(IoBridge.java:390)
01-07 23:34:36.787 2024-2024/com.sample.app W/System.err: ... 16 more
i have no idea about the problem.
Please help me. Thank you!!!
Related
I am running an app to test whether I can use Microsoft Translator API on a phone running Android API 19. However, when I press a button to get translated text, instead of translating the text "This is my dog" to French, Microsoft Translator API returns "".
UPDATE: Logcat is returning this:
02-07 07:13:52.148 28001-28250/com.example.inspiron.translat W/System.err: java.lang.Exception: [microsoft-translator-api] Error retrieving translation : Permission denied (missing INTERNET permission?)
02-07 07:13:52.148 28001-28250/com.example.inspiron.translat W/System.err: at com.memetix.mst.MicrosoftTranslatorAPI.retrieveString(MicrosoftTranslatorAPI.java:202)
02-07 07:13:52.148 28001-28250/com.example.inspiron.translat W/System.err: at com.memetix.mst.translate.Translate.execute(Translate.java:61)
02-07 07:13:52.148 28001-28250/com.example.inspiron.translat W/System.err: at com.example.inspiron.translat.MainActivity$1$1MyAsyncTask.doInBackground(MainActivity.java:45)
02-07 07:13:52.148 28001-28250/com.example.inspiron.translat W/System.err: at com.example.inspiron.translat.MainActivity$1$1MyAsyncTask.doInBackground(MainActivity.java:38)
02-07 07:13:52.148 28001-28250/com.example.inspiron.translat W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:288)
02-07 07:13:52.148 28001-28250/com.example.inspiron.translat W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-07 07:13:52.158 28001-28250/com.example.inspiron.translat W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
02-07 07:13:52.158 28001-28250/com.example.inspiron.translat W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-07 07:13:52.158 28001-28250/com.example.inspiron.translat W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: at java.lang.Thread.run(Thread.java:841)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:214)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:390)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:343)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:289)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:254)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: at com.memetix.mst.MicrosoftTranslatorAPI.getToken(MicrosoftTranslatorAPI.java:133)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: at com.memetix.mst.MicrosoftTranslatorAPI.retrieveResponse(MicrosoftTranslatorAPI.java:160)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: at com.memetix.mst.MicrosoftTranslatorAPI.retrieveString(MicrosoftTranslatorAPI.java:199)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: ... 9 more
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: at libcore.io.Posix.getaddrinfo(Native Method)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: ... 24 more
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: ... 27 more
02-07 07:15:59.518 29374-29374/com.example.inspiron.translat W/dalvikvm: VFY: unable to resolve virtual method 442: Landroid/content/Context;.getSystemService (Ljava/lang/Class;)Ljava/lang/Object;
02-07 07:15:59.518 29374-29374/com.example.inspiron.translat W/dalvikvm: VFY: unable to resolve virtual method 231: Landroid/app/Activity;.stopLockTask ()V
02-07 07:15:59.518 29374-29374/com.example.inspiron.translat E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.inspiron.translat.MainActivity.access$super
02-07 07:15:59.518 29374-29374/com.example.inspiron.translat W/dalvikvm: VFY: unable to resolve check-cast 227 (Landroid/os/PersistableBundle;) in Lcom/example/inspiron/translat/MainActivity;
02-07 07:15:59.518 29374-29374/com.example.inspiron.translat W/dalvikvm: VFY: unable to resolve virtual method 424:
My code is:
public class MainActivity extends AppCompatActivity {
TextView subject;
Button submit;
TextView result;
String stayalive;
String translatedText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
subject = (TextView) findViewById(R.id.textView);
submit = (Button) findViewById(R.id.button);
result = (TextView) findViewById(R.id.textView2);
Translate.setClientId("CLIENT ID");
Translate.setClientSecret("CLIENT SECRET");
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
class MyAsyncTask extends AsyncTask<DownloadManager.Request, Void, String> {
protected String doInBackground() {
try {
translatedText = Translate.execute("This is my dog", Language.AUTO_DETECT, Language.FRENCH);
return "";
} catch (Exception e) {
e.printStackTrace();
return "";
// I can't do a void
}
}
#Override
protected String doInBackground(DownloadManager.Request... params) {
return null;
}
#Override
protected void onPostExecute(String s) {
if (subject != null){
subject.setText(translatedText);
Context context = getApplicationContext();
CharSequence text = translatedText;
// the Toast didn't have any text in it
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
} else {
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
}
new MyAsyncTask().execute();
}
});
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.inspiron.translat">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<uses-permission android:name="android.permission.INTERNET"/>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Is the error because of my code or is it because of Microsoft Translator API itself? (I am using microsoft-translator-java-api-0.6.2-jar-with-dependencies.jar from https://code.google.com/archive/p/microsoft-translator-java-api/downloads.)
UPDATE 2: I got the answer from Permission denied (missing INTERNET permission?): But permission is given. Apparently I was supposed to put the <uses-permission> tags above application.
why you need two doInBackground call? Remove one and try
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
Button mBtn1 = (Button) findViewById(R.id.start_Quit_button);
mBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("clicks", "You Clicked B1");
Intent i = new Intent(
StartActivity.this,
MainActivity.class);
startActivity(i);
}
});
Button mBtn2 = (Button) findViewById(R.id.start_Next_button);
mBtn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("clicks", "You Clicked B2");
TextView username = (TextView) findViewById(R.id.login_text);
File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ExperimentLog");
if (!root.exists()) {
root.mkdir();
}
Username = username.getText().toString();
save(root, Username);
Intent i = new Intent(
StartActivity.this,
Test1_0.class);
startActivity(i);
}
});
}
public void save(File dir, String string) {
try {
File filepath = new File(dir, Username + ".txt");
FileWriter writer = new FileWriter(filepath);
writer.append(string);
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
This is the code I have. As you can see that i want the second button to go to the next activity and save the text I get from the editText start_Next_button. And a save method to write the string into the file created.
But the when I press the button on the AVD, android studio shows this:
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: java.io.FileNotFoundException: /sdcard/ExperimentLog/Name.txt: open failed: ENOENT (No such file or directory)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at libcore.io.IoBridge.open(IoBridge.java:452)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at java.io.FileWriter.<init>(FileWriter.java:42)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at com.android.mick.encodingaidingexperiment.StartActivity.save(StartActivity.java:74)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at com.android.mick.encodingaidingexperiment.StartActivity$2.onClick(StartActivity.java:61)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at android.view.View.performClick(View.java:5198)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at android.view.View$PerformClick.run(View.java:21147)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at android.os.Looper.loop(Looper.java:148)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at libcore.io.Posix.open(Native Method)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at libcore.io.IoBridge.open(IoBridge.java:438)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: ... 14 more
BTW I use AVD galaxy Nexus running android 6.0
Try adding a / after ExperimentLog and then mkdir() or mkdirs().
first of all the exception is about file is not exist, so you need create that folder and file if it doesn't exist on your directory.
you need a try catch block to handle FileNotFoundException.
and also you can use Files.createFile(thePath); to create file if not exist.
Just try this
File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ExperimentLog"+".txt");
instead of
File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ExperimentLog");
I tried to play a file in ListView with mediaPlayer class.
Like this:
public void initializeViews() {
Intent intent = getIntent();
String fName = intent.getStringExtra("fileName");
fileName = (TextView) findViewById(R.id.fileName);
duration = (TextView) findViewById(R.id.songDuration);
seekbar = (SeekBar) findViewById(R.id.seekBar);
playBtn = (ImageButton) findViewById(R.id.media_play);
try {
mediaPlayer = new android.media.MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(fName);
mediaPlayer.prepare();
}
catch (IOException e) {
e.printStackTrace();
}
fileName.setText(fName.substring(fName.lastIndexOf("/") + 1));
finalTime = mediaPlayer.getDuration();
seekbar.setOnSeekBarChangeListener(mOnSeek);
seekbar.setMax((int) finalTime);
seekbar.setClickable(true);
mediaPlayer.start();
timeElapsed = mediaPlayer.getCurrentPosition();
seekbar.setProgress((int) timeElapsed);
durationHandler.postDelayed(updateSeekBarTime, 200);
}
I search on the internet, and the most question is check the path to file, but the fName got the full path to a file. But I don't know it shows exception:
java.io.IOException: Prepare failed.: status=0x1
All log when application occurs exception:
E/MediaPlayer: error (1, -2147483648)
W/System.err: java.io.IOException: Prepare failed.: status=0x1
W/System.err: at android.media.MediaPlayer.prepare(Native Method)
W/System.err: at kr.co.composer.callrecord.media.AudioPlayer.initializeViews(AudioPlayer.java:56)
W/System.err: at kr.co.composer.callrecord.media.AudioPlayer.onCreate(AudioPlayer.java:42)
W/System.err: at android.app.Activity.performCreate(Activity.java:5231)
W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:135)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err: at android.os.Looper.loop(Looper.java:136)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5001)
W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
W/System.err: at dalvik.system.NativeStart.main(Native Method)
E/MediaPlayer: Attempt to call getDuration without a valid mediaplayer
E/MediaPlayer: error (-38, 0)
E/MediaPlayer: start called in state 0
E/MediaPlayer: error (-38, 0)
E/MediaPlayer: Error (-38,0)
E/MediaPlayer: Error (-38,0)
W/EGL_genymotion: eglSurfaceAttrib not implemented
stop called in state 0
error (-38, 0)
stop called in state 0
error (-38, 0)
Error (-38,0)
Error (-38,0)
stop called in state 0
error (-38, 0)
Error (-38,0)
pause called in state 0
error (-38, 0)
Possible caused by reading permission. Fix by inserting the permission in manifest file:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Patient patient = new Patient();
patient.setTitle(obj.getString("id"));
patient.setThumbnailUrl(obj.getString("image"));
patientList.add(patient);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
}
Logcat
01-07 07:27:29.294 7938-7938/info.androidhive.customlistviewvolley D/MainActivity: [{"id":"g"},{"image":"http:\/\/192.168.0.101\/test\/1.png"}]
01-07 07:27:29.312 7938-7938/info.androidhive.customlistviewvolley W/System.err: org.json.JSONException: No value for image
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at org.json.JSONObject.getString(JSONObject.java:550)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at info.androidhive.customlistviewvolley.MainActivity$1.onResponse(MainActivity.java:72)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at info.androidhive.customlistviewvolley.MainActivity$1.onResponse(MainActivity.java:59)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at android.os.Looper.loop(Looper.java:135)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5254)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at java.lang.reflect.Method.invoke(Native Method)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: org.json.JSONException: No value for id
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at org.json.JSONObject.getString(JSONObject.java:550)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at info.androidhive.customlistviewvolley.MainActivity$1.onResponse(MainActivity.java:71)
I can get data from the database but why does it still show that there is no value for the image?
I have checked that the url is right to get the photo. The following things are the error showed in the logcat. Please give me some helps. Thank you.
You get wrong JSONObject , try this
try {
JSONObject objId = response.getJSONObject(0);
JSONObject objImage = response.getJSONObject(1);
Patient patient = new Patient();
patient.setTitle(objId.getString("id"));
patient.setThumbnailUrl(objImage.getString("image"));
patientList.add(patient);
} catch (JSONException e) {
e.printStackTrace();
}
Look at your response and try this
JSONObject obj = response.getJSONObject(0);
JSONObject obj1 = response.getJSONObject(1);
Patient patient = new Patient();
patient.setTitle(obj.getString("id"));
patient.setThumbnailUrl(obj1.getString("image"));
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,