Google Play license check doCheck() method causes nullPointerException and force closes - java

The code below ends in a nullPointerException when run.
The logcat is first, the code is second.
Logcat:
07-27 09:58:13.705: D/AndroidRuntime(18164): Shutting down VM
0
7-27 09:58:13
.705: W/dalvikvm(18164): threadid=1: thread exiting with uncaught exception (group=0x40a3c1f8)
07-27 09:58:13.710: E/AndroidRuntime(18164): FATAL EXCEPTION: main
07-27 09:58:13.710: E/AndroidRuntime(18164): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.liamwli.smsbusy/com.liamwli.smsbusy.Sms_busyActivity}: java.lang.NullPointerException
07-27 09:58:13.710: E/AndroidRuntime(18164): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079)
07-27 09:58:13.710: E/AndroidRuntime(18164): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
07-27 09:58:13.710: E/AndroidRuntime(18164): at android.app.ActivityThread.access$600(ActivityThread.java:132)
07-27 09:58:13.710: E/AndroidRuntime(18164): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
07-27 09:58:13.710: E/AndroidRuntime(18164): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 09:58:13.710: E/AndroidRuntime(18164): at android.os.Looper.loop(Looper.java:137)
07-27 09:58:13.710: E/AndroidRuntime(18164): at android.app.ActivityThread.main(ActivityThread.java:4575)
07-27 09:58:13.710: E/AndroidRuntime(18164): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 09:58:13.710: E/AndroidRuntime(18164): at java.lang.reflect.Method.invoke(Method.java:511)
07-27 09:58:13.710: E/AndroidRuntime(18164): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-27 09:58:13.710: E/AndroidRuntime(18164): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-27 09:58:13.710: E/AndroidRuntime(18164): at dalvik.system.NativeStart.main(Native Method)
07-27 09:58:13.710: E/AndroidRuntime(18164): Caused by: java.lang.NullPointerException
07-27 09:58:13.710: E/AndroidRuntime(18164): at com.liamwli.smsbusy.Sms_busyActivity.doCheck(Sms_busyActivity.java:142)
07-27 09:58:13.710: E/AndroidRuntime(18164): at com.liamwli.smsbusy.Sms_busyActivity.onCreate(Sms_busyActivity.java:60)
07-27 09:58:13.710: E/AndroidRuntime(18164): at android.app.Activity.performCreate(Activity.java:4465)
07-27 09:58:13.710: E/AndroidRuntime(18164): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-27 09:58:13.710: E/AndroidRuntime(18164): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
07-27 09:58:13.710: E/AndroidRuntime(18164): ... 11 more
07-27 09:58:14.035: I/dalvikvm(18164): threadid=3: reacting to signal 3
07-27 09:58:14.055: I/dalvikvm(18164): Wrote stack traces to '/data/anr/traces.txt'
07-27 09:58:14.260: I/dalvikvm(18164): threadid=3: reacting to signal 3
07-27 09:58:14.275: I/dalvikvm(18164): Wrote stack traces to '/data/anr/traces.txt'
07-27 10:01:17.450: D/AndroidRuntime(18326): Shutting down VM
07-27 10:01:17.455: W/dalvikvm(18326): threadid=1: thread exiting with uncaught exception (group=0x40a3c1f8)
07-27 10:01:17.455: E/AndroidRuntime(18326): FATAL EXCEPTION: main
07-27 10:01:17.455: E/AndroidRuntime(18326): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.liamwli.smsbusy/com.liamwli.smsbusy.Sms_busyActivity}: java.lang.NullPointerException
07-27 10:01:17.455: E/AndroidRuntime(18326): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079)
07-27 10:01:17.455: E/AndroidRuntime(18326): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
07-27 10:01:17.455: E/AndroidRuntime(18326): at android.app.ActivityThread.access$600(ActivityThread.java:132)
07-27 10:01:17.455: E/AndroidRuntime(18326): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
07-27 10:01:17.455: E/AndroidRuntime(18326): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 10:01:17.455: E/AndroidRuntime(18326): at android.os.Looper.loop(Looper.java:137)
07-27 10:01:17.455: E/AndroidRuntime(18326): at android.app.ActivityThread.main(ActivityThread.java:4575)
07-27 10:01:17.455: E/AndroidRuntime(18326): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 10:01:17.455: E/AndroidRuntime(18326): at java.lang.reflect.Method.invoke(Method.java:511)
07-27 10:01:17.455: E/AndroidRuntime(18326): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-27 10:01:17.455: E/AndroidRuntime(18326): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-27 10:01:17.455: E/AndroidRuntime(18326): at dalvik.system.NativeStart.main(Native Method)
07-27 10:01:17.455: E/AndroidRuntime(18326): Caused by: java.lang.NullPointerException
07-27 10:01:17.455: E/AndroidRuntime(18326): at com.liamwli.smsbusy.Sms_busyActivity.doCheck(Sms_busyActivity.java:142)
07-27 10:01:17.455: E/AndroidRuntime(18326): at com.liamwli.smsbusy.Sms_busyActivity.onCreate(Sms_busyActivity.java:60)
07-27 10:01:17.455: E/AndroidRuntime(18326): at android.app.Activity.performCreate(Activity.java:4465)
07-27 10:01:17.455: E/AndroidRuntime(18326): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-27 10:01:17.455: E/AndroidRuntime(18326): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
07-27 10:01:17.455: E/AndroidRuntime(18326): ... 11 more
07-27 10:01:17.855: I/dalvikvm(18326): threadid=3: reacting to signal 3
07-27 10:01:17.860: I/dalvikvm(18326): Wrote stack traces to '/data/anr/traces.txt'
07-27 10:01:17.990: I/dalvikvm(18326): threadid=3: reacting to signal 3
07-27 10:01:17.995: I/dalvikvm(18326): Wrote stack traces to '/data/anr/traces.txt'
07-27 10:04:32.645: D/AndroidRuntime(18561): Shutting down VM
07-27 10:04:32.645: W/dalvikvm(18561): threadid=1: thread exiting with uncaught exception (group=0x40a3c1f8)
07-27 10:04:32.645: E/AndroidRuntime(18561): FATAL EXCEPTION: main
07-27 10:04:32.645: E/AndroidRuntime(18561): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.liamwli.smsbusy/com.liamwli.smsbusy.Sms_busyActivity}: java.lang.NullPointerException
07-27 10:04:32.645: E/AndroidRuntime(18561): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079)
07-27 10:04:32.645: E/AndroidRuntime(18561): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
07-27 10:04:32.645: E/AndroidRuntime(18561): at android.app.ActivityThread.access$600(ActivityThread.java:132)
07-27 10:04:32.645: E/AndroidRuntime(18561): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
07-27 10:04:32.645: E/AndroidRuntime(18561): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 10:04:32.645: E/AndroidRuntime(18561): at android.os.Looper.loop(Looper.java:137)
07-27 10:04:32.645: E/AndroidRuntime(18561): at android.app.ActivityThread.main(ActivityThread.java:4575)
07-27 10:04:32.645: E/AndroidRuntime(18561): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 10:04:32.645: E/AndroidRuntime(18561): at java.lang.reflect.Method.invoke(Method.java:511)
07-27 10:04:32.645: E/AndroidRuntime(18561): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-27 10:04:32.645: E/AndroidRuntime(18561): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-27 10:04:32.645: E/AndroidRuntime(18561): at dalvik.system.NativeStart.main(Native Method)
07-27 10:04:32.645: E/AndroidRuntime(18561): Caused by: java.lang.NullPointerException
07-27 10:04:32.645: E/AndroidRuntime(18561): at com.liamwli.smsbusy.Sms_busyActivity.doCheck(Sms_busyActivity.java:140)
07-27 10:04:32.645: E/AndroidRuntime(18561): at com.liamwli.smsbusy.Sms_busyActivity.onCreate(Sms_busyActivity.java:58)
07-27 10:04:32.645: E/AndroidRuntime(18561): at android.app.Activity.performCreate(Activity.java:4465)
07-27 10:04:32.645: E/AndroidRuntime(18561): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-27 10:04:32.645: E/AndroidRuntime(18561): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
07-27 10:04:32.645: E/AndroidRuntime(18561): ... 11 more
07-27 10:04:33.060: I/dalvikvm(18561): threadid=3: reacting to signal 3
07-27 10:04:33.065: I/dalvikvm(18561): Wrote stack traces to '/data/anr/traces.txt'
07-27 10:04:33.185: I/dalvikvm(18561): threadid=3: reacting to signal 3
07-27 10:04:33.215: I/dalvikvm(18561): Wrote stack traces to '/data/anr/traces.txt'
07-27 10:04:36.915: I/Process(18561): Sending signal. PID: 18561 SIG: 9
Line 140 is the mChecker.checkAccess line new line under the last bracket:
private void doCheck() {
mChecker.checkAccess(mLicenseCheckerCallback);
}
Line 58 is the call to the doCheck method:
doCheck();
The rest of the class is below:
package com.liamwli.smsbusy;
import com.google.android.vending.licensing.LicenseChecker;
import com.google.android.vending.licensing.LicenseCheckerCallback;
import com.google.android.vending.licensing.Policy;
import com.google.android.vending.licensing.StrictPolicy;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.provider.Settings.Secure;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
public class Sms_busyActivity extends Activity {
IntentFilter intentFilter;
ToggleButton endis;
EditText message;
Button smessage;
SharedPreferences getPrefs;
SharedPreferences.Editor editor;
private LicenseCheckerCallback mLicenseCheckerCallback;
private LicenseChecker mChecker;
private Handler mHandler;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String android_id = Secure.getString(getBaseContext()
.getContentResolver(), Secure.ANDROID_ID);
// if (!android_id.contentEquals("56d330123953677d")) {
// Log.e("SMS Busy App", "Device ID not allowed. Exiting.");
// finish();
// } else {
// Log.d("SMS Busy App", "Device ID Allowed");
// }
// Intent i = new Intent("com.liamwli.smsbusy.PREFS");
// startActivity(i);
setContentView(R.layout.def);
doCheck();
String KEY = "abcdefgijklmnopqrstuvqxyz"; //made up to post online
endis = (ToggleButton) findViewById(R.id.enableddis);
smessage = (Button) findViewById(R.id.savemess);
message = (EditText) findViewById(R.id.message);
getPrefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
editor = getPrefs.edit();
mLicenseCheckerCallback = new MyLicenseCheckerCallback();
final byte[] SALT = new byte[] { -46, 65, 30, -128, -103, -57, 74, -64,
51, 88, -95, -45, 77, -117, -36, -113, -11, 32, -64, 89 };
mChecker = new LicenseChecker(this, new StrictPolicy(), KEY);
mHandler = new Handler();
// ---intent to filter for SMS messages received---
intentFilter = new IntentFilter();
intentFilter.addAction("SMS_RECEIVED_ACTION");
Boolean state = getPrefs.getBoolean("enabled", false);
// String stext = getPrefs.getString("text", "");
// message.setText(stext);
message.setText(getPrefs.getString("text", ""));
if (message.getText().toString().contentEquals("")) {
Toast.makeText(this, "Unable to get saved message. Please resave.",
Toast.LENGTH_LONG).show();
}
endis.setChecked(state);
endis.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
Log.d("SMS Busy App", "onCheckedChanged Called");
// Toast.makeText(Sms_busyActivity.this, "App state changed",
// Toast.LENGTH_LONG).show();
editor = getPrefs.edit();
if (endis.isChecked()) {
editor.putBoolean("enabled", true);
} else {
editor.putBoolean("enabled", false);
}
editor.putString("text", message.getText().toString());
editor.commit();
}
});
smessage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
editor = getPrefs.edit();
editor.putString("text", message.getText().toString());
editor.commit();
Log.d("smessage", "Message saved & commited");
Toast.makeText(Sms_busyActivity.this, "Message Saved",
Toast.LENGTH_SHORT).show();
}
});
}
private void doCheck() {
mChecker.checkAccess(mLicenseCheckerCallback);
}
#SuppressWarnings("deprecation")
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (Integer.parseInt(android.os.Build.VERSION.SDK) < 5
&& keyCode == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() == 0) {
Log.d("SMS Busy App", "onKeyDown Called");
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
// public void onBackPressed() {
// Log.d("SMS Busy App", "onBackPressed Called");
// finish();
// }
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
Log.d("SMS Busy App", "onCreateOptionsMenu Called");
getMenuInflater().inflate(R.menu.mmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.aboutme:
Intent i = new Intent(this, AboutMe.class);
startActivity(i);
break;
}
return true;
}
private void displayResult(final String result) {
mHandler.post(new Runnable() {
public void run() {
Button mCheckLicenseButton = (Button) findViewById(R.id.button1);
mCheckLicenseButton.setText("App Licenced. Click to continue.");
mCheckLicenseButton.setEnabled(true);
setContentView(R.layout.main);
setProgressBarIndeterminateVisibility(false);
}
});
}
class MyLicenseCheckerCallback extends Activity implements
LicenseCheckerCallback {
public void allow(int reason) {
if (isFinishing()) {
// Don't update UI if Activity is finishing.
return;
}
// Should allow user access.
displayResult(getString(R.string.allow));
}
#SuppressWarnings("deprecation")
public void dontAllow(int reason) {
if (isFinishing()) {
// Don't update UI if Activity is finishing.
return;
}
AlertDialog alert = new AlertDialog.Builder(Sms_busyActivity.this)
.create();
if (reason == Policy.RETRY) {
// If the reason received from the policy is RETRY, it was
// probably
// due to a loss of connection with the service, so we should
// give the
// user a chance to retry. So show a dialog to retry.
alert.setTitle("Please retry license check");
alert.setMessage("The connection to the licensing server was lost. Please click retry below");
alert.setButton("Retry", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
});
alert.show();
} else {
// Otherwise, the user is not licensed to use this app.
// Your response should always inform the user that the
// application
// is not licensed, but your behavior at that point can vary.
// You might
// provide the user a limited access version of your app or you
// can
// take them to Google Play to purchase the app.f
setContentView(R.layout.def);
alert.setTitle("Not Licensed");
alert.setMessage("This app is not licensed. Please buy it off google play.");
alert.setButton("Exit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
});
alert.show();
}
}
#Override
public void applicationError(int errorCode) {
// TODO Auto-generated method stub
}
}
}
EDIT: Line numbers were off. New logcat generated. Please look again.

You initialise you mChecker after using it :
mChecker = new LicenseChecker(this, new StrictPolicy(), KEY);
mLicenseCheckerCallback = new MyLicenseCheckerCallback();
doCheck();
String KEY = "abcdefgijklmnopqrstuvqxyz"; //made up to post online
endis = (ToggleButton) findViewById(R.id.enableddis);
smessage = (Button) findViewById(R.id.savemess);
message = (EditText) findViewById(R.id.message);
getPrefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
editor = getPrefs.edit();
final byte[] SALT = new byte[] { -46, 65, 30, -128, -103, -57, 74, -64,
51, 88, -95, -45, 77, -117, -36, -113, -11, 32, -64, 89 };

put below line before you call doCheck() method
mChecker = new LicenseChecker(this, new StrictPolicy(), KEY);
accoding to your onCreate(), it should look like
setContentView(R.layout.def);
String KEY = "abcdefgijklmnopqrstuvqxyz"; //Because u are using key in next line hence move Key before next line
mChecker = new LicenseChecker(this, new StrictPolicy(), KEY);// <<<THIS LINE SHOULD COME BEFORE doCheck CALLED
doCheck();
and setContentView(R.layout.def); should be moved just after super call in oncreate.

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;

Android: Matching string to Color within a Hashset- displaying randomly on screen?

I am trying to create an android game, similar to the STROOP EFFECT.
In my first step I am attempting to create a HashMap with strings of colors as the key and their corresponding android color as the value. I.e:
HashMap<String, Integer> colors= new HashMap<>();
colors.put("Green", Color.GREEN);
colors.put("Blue", Color.BLUE);
colors.put("Red", Color.RED);
colors.put("Yellow", Color.YELLOW);
colors.put("Black", Color.BLACK);
I then convert both the keyset and the values to an array. (see code below)
What I am having trouble implementing is setting the Textview (where the string will be shown) to show a random String from the array and also to set this string to a random color from the color array. When I attempt to do my app crashes and I get the following error:
07-27 22:36:00.192: E/AndroidRuntime(32079): FATAL EXCEPTION: main
07-27 22:36:00.192: E/AndroidRuntime(32079): Process: com.example.brianapp, PID: 32079
07-27 22:36:00.192: E/AndroidRuntime(32079): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.brianapp/com.example.brianapp.Stroop}: java.lang.ArrayIndexOutOfBoundsException: length=0; index=2
07-27 22:36:00.192: E/AndroidRuntime(32079): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
07-27 22:36:00.192: E/AndroidRuntime(32079): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
Current activity code:
public class Stroop extends ActionBarActivity {
HashMap<String, Integer> colors= new HashMap<>();
//putting the strings of the hasmap to an array
Object stringOnScreen[]= colors.keySet().toArray();
Object colorsOnScreen[]= colors.values().toArray();
TextView color;
Button btn1;
Button btn2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stroop);
Log.d("test", "test: " + stringOnScreen[2].toString()); //trace code
}//oncreate end
public void setUpQuestion(){
//set the text of the string in textview for user to see
color.setText("" + stringOnScreen[randInt(0, 4)]);
color.setTextColor((int) colorsOnScreen[randInt(0,4)]);
}
public void setUpGame(){
// setting up the hashmap
colors.put("Green", Color.GREEN);
colors.put("Blue", Color.BLUE);
colors.put("Red", Color.RED);
colors.put("Yellow", Color.YELLOW);
colors.put("Black", Color.BLACK);
// setting up vars
color = (TextView) findViewById(R.id.tvStroopColor);
btn1 = (Button) findViewById(R.id.btnStroop1);
btn2 = (Button) findViewById(R.id.btnStroop2);
}
/**
* method to get a random int
* #param min
* #param max
* #return
*/
public static int randInt(int min, int max) {
// NOTE: Usually this should be a field rather than a method
// variable so that it is not re-seeded every call.
Random rand = new Random();
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
}
How can I implement this functionality? Any help would be great!
ATTEMPTED SOLUTION:
public class Stroop extends ActionBarActivity {
HashMap<String, Integer> colors= new HashMap<>();
//putting the strings of the hahsmap to an array
Object stringOnScreen[]= colors.keySet().toArray();
Object colorsOnScreen[]= colors.values().toArray();
TextView color;
Button btn1;
Button btn2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stroop);
setUpGame();
setUpQuestion();
}//oncreate end
public void setUpQuestion(){
int randString= new Random().nextInt(stringOnScreen.length);
int randColor= new Random().nextInt(colorsOnScreen.length);
//set the text of the string in textview for user to see
color.setText("" + stringOnScreen[randString]);
color.setTextColor(randColor);
}
public void setUpGame(){
// setting up the hashmap
colors.put("Green", Color.GREEN);
colors.put("Blue", Color.BLUE);
colors.put("Red", Color.RED);
colors.put("Yellow", Color.YELLOW);
colors.put("Black", Color.BLACK);
// setting up vars
color = (TextView) findViewById(R.id.tvStroopColor);
btn1 = (Button) findViewById(R.id.btnStroop1);
btn2 = (Button) findViewById(R.id.btnStroop2);
}
}
Now getting the following Logcat error report:
07-27 23:11:22.482: E/AndroidRuntime(11902): FATAL EXCEPTION: main
07-27 23:11:22.482: E/AndroidRuntime(11902): Process: com.example.brianapp, PID: 11902
07-27 23:11:22.482: E/AndroidRuntime(11902): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.brianapp/com.example.brianapp.Stroop}: java.lang.IllegalArgumentException: n <= 0: 0
07-27 23:11:22.482: E/AndroidRuntime(11902): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
07-27 23:11:22.482: E/AndroidRuntime(11902): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
07-27 23:11:22.482: E/AndroidRuntime(11902): at android.app.ActivityThread.access$900(ActivityThread.java:161)
07-27 23:11:22.482: E/AndroidRuntime(11902): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
07-27 23:11:22.482: E/AndroidRuntime(11902): at android.os.Handler.dispatchMessage(Handler.java:102)
07-27 23:11:22.482: E/AndroidRuntime(11902): at android.os.Looper.loop(Looper.java:157)
07-27 23:11:22.482: E/AndroidRuntime(11902): at android.app.ActivityThread.main(ActivityThread.java:5356)
07-27 23:11:22.482: E/AndroidRuntime(11902): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 23:11:22.482: E/AndroidRuntime(11902): at java.lang.reflect.Method.invoke(Method.java:515)
07-27 23:11:22.482: E/AndroidRuntime(11902): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
07-27 23:11:22.482: E/AndroidRuntime(11902): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
07-27 23:11:22.482: E/AndroidRuntime(11902): at dalvik.system.NativeStart.main(Native Method)
07-27 23:11:22.482: E/AndroidRuntime(11902): Caused by: java.lang.IllegalArgumentException: n <= 0: 0
07-27 23:11:22.482: E/AndroidRuntime(11902): at java.util.Random.nextInt(Random.java:175)
07-27 23:11:22.482: E/AndroidRuntime(11902): at com.example.brianapp.Stroop.setUpQuestion(Stroop.java:51)
07-27 23:11:22.482: E/AndroidRuntime(11902): at com.example.brianapp.Stroop.onCreate(Stroop.java:42)
07-27 23:11:22.482: E/AndroidRuntime(11902): at android.app.Activity.performCreate(Activity.java:5426)
07-27 23:11:22.482: E/AndroidRuntime(11902): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
07-27 23:11:22.482: E/AndroidRuntime(11902): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
07-27 23:11:22.482: E/AndroidRuntime(11902): ... 11 more
Note lines:
07-27 23:11:22.482: E/AndroidRuntime(11902): Caused by: java.lang.IllegalArgumentException: n <= 0: 0
07-27 23:11:22.482: E/AndroidRuntime(11902): at java.util.Random.nextInt(Random.java:175)
07-27 23:11:22.482: E/AndroidRuntime(11902): at com.example.brianapp.Stroop.setUpQuestion(Stroop.java:51)
07-27 23:11:22.482: E/AndroidRuntime(11902): at
As your log says, your array index is out of bounds, because your array is empty. Initialize stringOnScreen and colorOnScreen AFTER calling setUpGame and do both before accessing the array.
public class Stroop extends ActionBarActivity {
HashMap<String, Integer> colors = new HashMap<>();
Object stringOnScreen[];
Object colorsOnScreen[];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stroop);
setUpGame()
stringOnScreen = colors.keySet().toArray();
colorOnScreen = colors.values().toArray();
Log.d("test", "test: " + stringOnScreen[2].toString());
}
...
}

android:get java.io.EOFException on press button

i have a button in my app that i want to when i press button app send a string to websrvice and get the result , this is my code :
btn = (Button) findViewById(R.id.button_add_to_cart);
btn.setTypeface(typeface);
btn.setEnabled(false);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
env = new SoapSerializationEnvelope(SoapEnvelope.VER11);
env.dotNet = false;
env.xsd = SoapSerializationEnvelope.XSD;
env.enc = SoapSerializationEnvelope.ENC;
env.encodingStyle="utf-8";
request = new SoapObject("customWebService",
"add");
request.addProperty("sessionID", sessionId);
env.setOutputSoapObject(request);
androidHttpTransport = new HttpTransportSE(
"http://mywebsiteee.com/WebService/server.php?wsdl/",
60000);
androidHttpTransport.debug = true;
androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
androidHttpTransport.call("", env);
SoapObject result = (SoapObject)env.bodyIn;
String Test1 = result.getPropertyAsString(0);
String str = env.bodyIn.toString();
String s = Test1;
byte[] b = s.getBytes("UTF-8");
s = new String(b, "UTF-8");
try {
JSONObject jsonObject = new JSONObject(Test1);
s = jsonObject.getString("result");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
my problem is when i press button first time i get this error :
java.io.EOFException
this is full log :
07-27 07:47:25.059: W/System.err(1251): java.io.EOFException
07-27 07:47:25.059: W/System.err(1251): at libcore.io.Streams.readAsciiLine(Streams.java:203)
07-27 07:47:25.059: W/System.err(1251): at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:579)
07-27 07:47:25.059: W/System.err(1251): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:827)
07-27 07:47:25.059: W/System.err(1251): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
07-27 07:47:25.059: W/System.err(1251): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497)
07-27 07:47:25.059: W/System.err(1251): at org.ksoap2.transport.ServiceConnectionSE.getResponseCode(ServiceConnectionSE.java:103)
07-27 07:47:25.059: W/System.err(1251): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:197)
07-27 07:47:25.059: W/System.err(1251): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118)
07-27 07:47:25.059: W/System.err(1251): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:113)
07-27 07:47:25.059: W/System.err(1251): at com.tashilgostar.tashilgostarstore.ProductActivity$5.onClick(ProductActivity.java:294)
07-27 07:47:25.059: W/System.err(1251): at android.view.View.performClick(View.java:4240)
07-27 07:47:25.059: W/System.err(1251): at android.view.View$PerformClick.run(View.java:17721)
07-27 07:47:25.059: W/System.err(1251): at android.os.Handler.handleCallback(Handler.java:730)
07-27 07:47:25.059: W/System.err(1251): at android.os.Handler.dispatchMessage(Handler.java:92)
07-27 07:47:25.059: W/System.err(1251): at android.os.Looper.loop(Looper.java:137)
07-27 07:47:25.059: W/System.err(1251): at android.app.ActivityThread.main(ActivityThread.java:5103)
07-27 07:47:25.059: W/System.err(1251): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 07:47:25.059: W/System.err(1251): at java.lang.reflect.Method.invoke(Method.java:525)
07-27 07:47:25.059: W/System.err(1251): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-27 07:47:25.059: W/System.err(1251): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-27 07:47:25.059: W/System.err(1251): at dalvik.system.NativeStart.main(Native Method)
but after some time i press button or when i press it long time it`s work correctly !
can any body help me know why this happen ?
It worked setting the header property "Connection".
ArrayList<HeaderProperty> headerPropertyArrayList = new ArrayList<HeaderProperty>();
headerPropertyArrayList.add(new HeaderProperty("Connection", "close"));
transport.call(SOAP_ACTION, envelope, headerPropertyArrayList);
https://code.google.com/p/ksoap2-android/issues/detail?id=173&can=1&start=100

pdf extracting with iText error occured

I want to extract the text from a pdf-file on the internet to a string in java using iText. This is my code:
package me.moop.mytwitter;
import java.io.IOException;
import java.text.ParseException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.app.ProgressDialog;
public class MainActivity extends Activity {
Button mBtnCheck;
EditText mEtxtGroup;
ProgressDialog mProgressDialog;
TextView mTxtv1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nicelayout3);
mBtnCheck = (Button) findViewById(R.id.btnCheck);
mEtxtGroup = (EditText) findViewById(R.id.etxtGroup);
mTxtv1 = (TextView) findViewById(R.id.textView1);
}
public void checkScheduleChange(View view){
if (view == mBtnCheck){
String group;
group = mEtxtGroup.getText().toString();
if (group.length() > 0){
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Bezig met checken voor roosterwijzigingen...");
mProgressDialog.show();
new CheckScheduleChangeTask().execute();
}
else{
Toast.makeText(this, "Voer een klas in", Toast.LENGTH_LONG).show();
}
}
}
private class CheckScheduleChangeTask extends AsyncTask<Void, Void, Void> {
String content;
#Override
protected Void doInBackground(Void... args) {
PdfReader reader = null;
try {
reader = new PdfReader("http://www.augustinianum.eu/roosterwijzigingen/14062012.pdf");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
content = PdfTextExtractor.getTextFromPage(reader, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void arg) {
mProgressDialog.dismiss();
String group = mEtxtGroup.getText().toString();
if (content.contains(group)){
Toast.makeText(MainActivity.this, "U hebt een roosterwijziging", Toast.LENGTH_LONG).show();
}
mTxtv1.setText(content);
}
}
}
For the people in a hurry, here's the iText part:
private class CheckScheduleChangeTask extends AsyncTask<Void, Void, Void> {
String content;
#Override
protected Void doInBackground(Void... args) {
PdfReader reader = null;
try {
reader = new PdfReader("http://www.augustinianum.eu/roosterwijzigingen/14062012.pdf");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
content = PdfTextExtractor.getTextFromPage(reader, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void arg) {
// more code
}
Whenever I launch this in my emulator I'm getting a fore close after calling the class: CheckScheduleChangeTask. This is the log:
07-27 09:15:08.354: D/ddm-heap(222): Got feature list request
07-27 09:15:12.994: E/dalvikvm(222): Could not find class 'com.itextpdf.text.pdf.PdfReader', referenced from method me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground
07-27 09:15:13.004: W/dalvikvm(222): VFY: unable to resolve new-instance 410 (Lcom/itextpdf/text/pdf/PdfReader;) in Lme/moop/mytwitter/MainActivity$CheckScheduleChangeTask;
07-27 09:15:13.004: D/dalvikvm(222): VFY: replacing opcode 0x22 at 0x0002
07-27 09:15:13.004: D/dalvikvm(222): Making a copy of Lme/moop/mytwitter/MainActivity$CheckScheduleChangeTask;.doInBackground code (137 bytes)
07-27 09:15:13.004: E/dalvikvm(222): Could not find class 'com.itextpdf.text.pdf.parser.PdfReaderContentParser', referenced from method me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground
07-27 09:15:13.004: W/dalvikvm(222): VFY: unable to resolve new-instance 411 (Lcom/itextpdf/text/pdf/parser/PdfReaderContentParser;) in Lme/moop/mytwitter/MainActivity$CheckScheduleChangeTask;
07-27 09:15:13.004: D/dalvikvm(222): VFY: replacing opcode 0x22 at 0x000a
07-27 09:15:13.045: W/dalvikvm(222): threadid=15: thread exiting with uncaught exception (group=0x4001b188)
07-27 09:15:13.045: E/AndroidRuntime(222): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
07-27 09:15:13.135: E/AndroidRuntime(222): java.lang.RuntimeException: An error occured while executing doInBackground()
07-27 09:15:13.135: E/AndroidRuntime(222): at android.os.AsyncTask$3.done(AsyncTask.java:200)
07-27 09:15:13.135: E/AndroidRuntime(222): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-27 09:15:13.135: E/AndroidRuntime(222): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-27 09:15:13.135: E/AndroidRuntime(222): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-27 09:15:13.135: E/AndroidRuntime(222): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-27 09:15:13.135: E/AndroidRuntime(222): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
07-27 09:15:13.135: E/AndroidRuntime(222): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
07-27 09:15:13.135: E/AndroidRuntime(222): at java.lang.Thread.run(Thread.java:1096)
07-27 09:15:13.135: E/AndroidRuntime(222): Caused by: java.lang.NoClassDefFoundError: com.itextpdf.text.pdf.PdfReader
07-27 09:15:13.135: E/AndroidRuntime(222): at me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground(MainActivity.java:84)
07-27 09:15:13.135: E/AndroidRuntime(222): at me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground(MainActivity.java:1)
07-27 09:15:13.135: E/AndroidRuntime(222): at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-27 09:15:13.135: E/AndroidRuntime(222): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-27 09:15:13.135: E/AndroidRuntime(222): ... 4 more
07-27 09:15:13.164: I/dalvikvm(222): threadid=7: reacting to signal 3
07-27 09:15:13.394: I/dalvikvm(222): Wrote stack trace to '/data/anr/traces.txt'
07-27 09:15:16.894: I/Process(222): Sending signal. PID: 222 SIG: 9
07-27 09:17:59.094: E/dalvikvm(252): Could not find class 'com.itextpdf.text.pdf.PdfReader', referenced from method me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground
07-27 09:17:59.094: W/dalvikvm(252): VFY: unable to resolve new-instance 410 (Lcom/itextpdf/text/pdf/PdfReader;) in Lme/moop/mytwitter/MainActivity$CheckScheduleChangeTask;
07-27 09:17:59.094: D/dalvikvm(252): VFY: replacing opcode 0x22 at 0x0002
07-27 09:17:59.094: D/dalvikvm(252): Making a copy of Lme/moop/mytwitter/MainActivity$CheckScheduleChangeTask;.doInBackground code (137 bytes)
07-27 09:17:59.094: E/dalvikvm(252): Could not find class 'com.itextpdf.text.pdf.parser.PdfReaderContentParser', referenced from method me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground
07-27 09:17:59.094: W/dalvikvm(252): VFY: unable to resolve new-instance 411 (Lcom/itextpdf/text/pdf/parser/PdfReaderContentParser;) in Lme/moop/mytwitter/MainActivity$CheckScheduleChangeTask;
07-27 09:17:59.094: D/dalvikvm(252): VFY: replacing opcode 0x22 at 0x000a
07-27 09:17:59.124: W/dalvikvm(252): threadid=15: thread exiting with uncaught exception (group=0x4001b188)
07-27 09:17:59.124: E/AndroidRuntime(252): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
07-27 09:17:59.134: E/AndroidRuntime(252): java.lang.RuntimeException: An error occured while executing doInBackground()
07-27 09:17:59.134: E/AndroidRuntime(252): at android.os.AsyncTask$3.done(AsyncTask.java:200)
07-27 09:17:59.134: E/AndroidRuntime(252): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-27 09:17:59.134: E/AndroidRuntime(252): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-27 09:17:59.134: E/AndroidRuntime(252): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-27 09:17:59.134: E/AndroidRuntime(252): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-27 09:17:59.134: E/AndroidRuntime(252): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
07-27 09:17:59.134: E/AndroidRuntime(252): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
07-27 09:17:59.134: E/AndroidRuntime(252): at java.lang.Thread.run(Thread.java:1096)
07-27 09:17:59.134: E/AndroidRuntime(252): Caused by: java.lang.NoClassDefFoundError: com.itextpdf.text.pdf.PdfReader
07-27 09:17:59.134: E/AndroidRuntime(252): at me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground(MainActivity.java:70)
07-27 09:17:59.134: E/AndroidRuntime(252): at me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground(MainActivity.java:1)
07-27 09:17:59.134: E/AndroidRuntime(252): at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-27 09:17:59.134: E/AndroidRuntime(252): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-27 09:17:59.134: E/AndroidRuntime(252): ... 4 more
07-27 09:17:59.521: I/dalvikvm(252): threadid=7: reacting to signal 3
07-27 09:17:59.794: I/dalvikvm(252): Wrote stack trace to '/data/anr/traces.txt'
07-27 09:18:02.743: I/Process(252): Sending signal. PID: 252 SIG: 9
07-27 09:26:26.713: E/dalvikvm(280): Could not find class 'com.itextpdf.text.pdf.PdfReader', referenced from method me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground
07-27 09:26:26.723: W/dalvikvm(280): VFY: unable to resolve new-instance 410 (Lcom/itextpdf/text/pdf/PdfReader;) in Lme/moop/mytwitter/MainActivity$CheckScheduleChangeTask;
07-27 09:26:26.723: D/dalvikvm(280): VFY: replacing opcode 0x22 at 0x0001
07-27 09:26:26.723: D/dalvikvm(280): Making a copy of Lme/moop/mytwitter/MainActivity$CheckScheduleChangeTask;.doInBackground code (93 bytes)
07-27 09:26:26.723: I/dalvikvm(280): Could not find method com.itextpdf.text.pdf.parser.PdfTextExtractor.getTextFromPage, referenced from method me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground
07-27 09:26:26.723: W/dalvikvm(280): VFY: unable to resolve static method 2969: Lcom/itextpdf/text/pdf/parser/PdfTextExtractor;.getTextFromPage (Lcom/itextpdf/text/pdf/PdfReader;I)Ljava/lang/String;
07-27 09:26:26.723: D/dalvikvm(280): VFY: replacing opcode 0x71 at 0x000a
07-27 09:26:26.754: W/dalvikvm(280): threadid=15: thread exiting with uncaught exception (group=0x4001b188)
07-27 09:26:26.754: E/AndroidRuntime(280): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
07-27 09:26:26.763: E/AndroidRuntime(280): java.lang.RuntimeException: An error occured while executing doInBackground()
07-27 09:26:26.763: E/AndroidRuntime(280): at android.os.AsyncTask$3.done(AsyncTask.java:200)
07-27 09:26:26.763: E/AndroidRuntime(280): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-27 09:26:26.763: E/AndroidRuntime(280): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-27 09:26:26.763: E/AndroidRuntime(280): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-27 09:26:26.763: E/AndroidRuntime(280): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-27 09:26:26.763: E/AndroidRuntime(280): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
07-27 09:26:26.763: E/AndroidRuntime(280): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
07-27 09:26:26.763: E/AndroidRuntime(280): at java.lang.Thread.run(Thread.java:1096)
07-27 09:26:26.763: E/AndroidRuntime(280): Caused by: java.lang.NoClassDefFoundError: com.itextpdf.text.pdf.PdfReader
07-27 09:26:26.763: E/AndroidRuntime(280): at me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground(MainActivity.java:68)
07-27 09:26:26.763: E/AndroidRuntime(280): at me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground(MainActivity.java:1)
07-27 09:26:26.763: E/AndroidRuntime(280): at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-27 09:26:26.763: E/AndroidRuntime(280): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-27 09:26:26.763: E/AndroidRuntime(280): ... 4 more
07-27 09:26:27.063: I/dalvikvm(280): threadid=7: reacting to signal 3
07-27 09:26:27.404: I/dalvikvm(280): Wrote stack trace to '/data/anr/traces.txt'
07-27 09:46:50.573: E/dalvikvm(308): Could not find class 'com.itextpdf.text.pdf.PdfReader', referenced from method me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground
07-27 09:46:50.573: W/dalvikvm(308): VFY: unable to resolve new-instance 410 (Lcom/itextpdf/text/pdf/PdfReader;) in Lme/moop/mytwitter/MainActivity$CheckScheduleChangeTask;
07-27 09:46:50.573: D/dalvikvm(308): VFY: replacing opcode 0x22 at 0x0001
07-27 09:46:50.573: D/dalvikvm(308): Making a copy of Lme/moop/mytwitter/MainActivity$CheckScheduleChangeTask;.doInBackground code (97 bytes)
07-27 09:46:50.573: I/dalvikvm(308): Could not find method com.itextpdf.text.pdf.parser.PdfTextExtractor.getTextFromPage, referenced from method me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground
07-27 09:46:50.573: W/dalvikvm(308): VFY: unable to resolve static method 2969: Lcom/itextpdf/text/pdf/parser/PdfTextExtractor;.getTextFromPage (Lcom/itextpdf/text/pdf/PdfReader;I)Ljava/lang/String;
07-27 09:46:50.583: D/dalvikvm(308): VFY: replacing opcode 0x71 at 0x000a
07-27 09:46:50.624: W/dalvikvm(308): threadid=15: thread exiting with uncaught exception (group=0x4001b188)
07-27 09:46:50.624: E/AndroidRuntime(308): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
07-27 09:46:50.634: E/AndroidRuntime(308): java.lang.RuntimeException: An error occured while executing doInBackground()
07-27 09:46:50.634: E/AndroidRuntime(308): at android.os.AsyncTask$3.done(AsyncTask.java:200)
07-27 09:46:50.634: E/AndroidRuntime(308): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-27 09:46:50.634: E/AndroidRuntime(308): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-27 09:46:50.634: E/AndroidRuntime(308): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-27 09:46:50.634: E/AndroidRuntime(308): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-27 09:46:50.634: E/AndroidRuntime(308): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
07-27 09:46:50.634: E/AndroidRuntime(308): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
07-27 09:46:50.634: E/AndroidRuntime(308): at java.lang.Thread.run(Thread.java:1096)
07-27 09:46:50.634: E/AndroidRuntime(308): Caused by: java.lang.NoClassDefFoundError: com.itextpdf.text.pdf.PdfReader
07-27 09:46:50.634: E/AndroidRuntime(308): at me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground(MainActivity.java:68)
07-27 09:46:50.634: E/AndroidRuntime(308): at me.moop.mytwitter.MainActivity$CheckScheduleChangeTask.doInBackground(MainActivity.java:1)
07-27 09:46:50.634: E/AndroidRuntime(308): at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-27 09:46:50.634: E/AndroidRuntime(308): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-27 09:46:50.634: E/AndroidRuntime(308): ... 4 more
07-27 09:46:51.105: I/dalvikvm(308): threadid=7: reacting to signal 3
07-27 09:46:51.423: I/dalvikvm(308): Wrote stack trace to '/data/anr/traces.txt'
07-27 09:46:54.444: I/Process(308): Sending signal. PID: 308 SIG: 9
It seems like there's something wrong with the imports. Does anyone know how to fix this?
THANK YOU!
It did it using iText, it's extremely easy. However, if someone has a question about it, just ask me.
BTW The solution of the question above is here:
https://stackoverflow.com/a/10046725/1289716
You can use this simple example of creating pdf from text :)
Use the library droidtext0.2jar .. available on the site .
Edited
you added your library to build path ? If not then add it .
You might need to import these as well:
import com.lowagie.text.BadElementException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;

Buttons on Layout with Webview

I cannot figure out why these 2 buttons are not working, i have a layout file inwhich is created when the user selects a certain theme in my application. With the layout it has a webview it has 2 buttons to goback and goforward.
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:text="Back"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:background="#drawable/button_blue"
style="#style/ButtonText">
</Button>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/button_blue"
style="#style/ButtonText"
android:text="Forward">
</Button>
<WebView
android:id="#+id/webview01"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1.12" >
This is not the entire layout file, but here is the bit of stuff i am working with for the webview and the 2 buttons, now inside my main activity here is my button code.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Prefs.theme.equals("Theme1"))
setContentView(R.layout.main);
else if (Prefs.theme.equals("Theme2"))
setContentView(R.layout.main2);
else if (Prefs.theme.equals("Theme3"))
setContentView(R.layout.main3);
else setContentView(R.layout.main);
btnForward=(Button) findViewById (R.id.button1);
btnBackward=(Button) findViewById (R.id.button2);
btnForward.setOnClickListener(this);
btnBackward.setOnClickListener(this);
// more code within on create .....
// Later in the code
public void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
WebViewClientDemoActivity.web.goBack();
break;
case R.id.button2:
WebViewClientDemoActivity.web.goForward();
break;
}
//
I problem im having is by default it loads main.xml not main3 (which is were the 2 buttons are)
LogCat Errors
07-27 16:00:34.802: E/AndroidRuntime(547): FATAL EXCEPTION: main
07-27 16:00:34.802: E/AndroidRuntime(547): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jaisonbrooks.enlighten/com.jaisonbrooks.enlighten.WebViewClientDemoActivity}: java.lang.NullPointerException
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.os.Looper.loop(Looper.java:123)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-27 16:00:34.802: E/AndroidRuntime(547): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 16:00:34.802: E/AndroidRuntime(547): at java.lang.reflect.Method.invoke(Method.java:507)
07-27 16:00:34.802: E/AndroidRuntime(547): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-27 16:00:34.802: E/AndroidRuntime(547): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-27 16:00:34.802: E/AndroidRuntime(547): at dalvik.system.NativeStart.main(Native Method)
07-27 16:00:34.802: E/AndroidRuntime(547): Caused by: java.lang.NullPointerException
07-27 16:00:34.802: E/AndroidRuntime(547): at com.jaisonbrooks.enlighten.WebViewClientDemoActivity.onCreate(WebViewClientDemoActivity.java:75)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-27 16:00:34.802: E/AndroidRuntime(547): ... 11 more
First in your onCreate method find your buttons by ids :
public class YourActivity extends Activity implements OnClickListener {
Button btnForward;
Button btnBackward;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnForward=(Button) findViewById (R.id.btnForward);
btnBackward=(Button) findViewById (R.id.btnBackward);
//set listeners
btnForward.setOnClickListener(this);
btnBackward.setOnClickListener(this);
// your code here ....
}
#Override
public void onClick(View v ) {
switch(v.getId()) {
case R.id.btnBackward:
WebViewClientDemoActivity.web.goBack();
break;
case R.id.btnForward:
WebViewClientDemoActivity.web.goForward();
break;
}
}
}
It should work. Make sure you don't have 2 buttons with the same #id.
It's a common issue when you multiplicate your buttons from one. (copy/paste)
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:text="Back"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:background="#drawable/button_blue"
style="#style/ButtonText" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/button_blue"
style="#style/ButtonText"
android:text="Forward" />
try this.
then later in java code, do this
Button btnNext = (Button) findViewById(R.id.button2);
btnNext.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
WebViewClientDemoActivity.web.goForward();
}
});
Button btnBack = (Button) findViewById(R.id.button1);
btnNext.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
WebViewClientDemoActivity.web.goBack();
}
});

Categories