I have an ArrayList of a custom object with lat and long properties.
I have managed to put markers on the map. What I want is to be able to zoom in to those markers when the map loads.
What I have tried below causes the app to crash.
Here is the code for loading the map:
public void getBridgeData() {
db = new DbHelperClass(this);
bridges = db.getAllBridges();
DecimalFormat dc = new DecimalFormat("#.000");
Builder builder = new LatLngBounds.Builder();
for (int i= 0;i<bridges.size();i++) {
Bridge b= (Bridge)bridges.get(i);
double lati= b.getLatitude();
double longo= b.getLongitude();
double reflat= b.getReflat();
double reflong= b.getReflong();
LatLng start = new LatLng(reflat,reflong);
LatLng end = new LatLng(lati,longo);
GeneralUtils uts = new GeneralUtils();
double ch= uts.calculateDistance(start, end);
String chainage = dc.format(ch);
String mysnipet = "Location:" + b.getRoadname() + " " + chainage;
Marker mm=map.addMarker(new
MarkerOptions().position(new LatLng(lati, longo))
.title(b.getBridgename())
.snippet(mysnipet));
builder.include(mm.getPosition());
}
LatLngBounds bounds= builder.build();
map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 15));
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}
Here also part of the code
public class MainActivity extends FragmentActivity {
private GoogleMap map;
private DbHelperClass db;
private List<Bridge> bridges = new ArrayList<Bridge>();
CameraUpdate cu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_map);
//AddSomeData();
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
if(map!=null) {
getBridgeData();
}
}
}
Here is the error log:
05-30 22:19:28.894: E/AndroidRuntime(5593): FATAL EXCEPTION: main
05-30 22:19:28.894: E/AndroidRuntime(5593): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bridge.bridgeinventory/com.bridge.bridgeinventory.MainActivity}: java.lang.IllegalStateException: Error using newLatLngBounds(LatLngBounds, int): Map size can't be 0. Most likely, layout has not yet occured for the map view. Either wait until layout has occurred or use newLatLngBounds(LatLngBounds, int, int, int) which allows you to specify the map's dimensions.
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1755)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1774)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.ActivityThread.access$1500(ActivityThread.java:157)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1001)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.os.Handler.dispatchMessage(Handler.java:130)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.os.Looper.loop(SourceFile:351)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.ActivityThread.main(ActivityThread.java:3841)
05-30 22:19:28.894: E/AndroidRuntime(5593): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 22:19:28.894: E/AndroidRuntime(5593): at java.lang.reflect.Method.invoke(Method.java:538)
05-30 22:19:28.894: E/AndroidRuntime(5593): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969)
05-30 22:19:28.894: E/AndroidRuntime(5593): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727)
05-30 22:19:28.894: E/AndroidRuntime(5593): at dalvik.system.NativeStart.main(Native Method)
05-30 22:19:28.894: E/AndroidRuntime(5593): Caused by: java.lang.IllegalStateException: Error using newLatLngBounds(LatLngBounds, int): Map size can't be 0. Most likely, layout has not yet occured for the map view. Either wait until layout has occurred or use newLatLngBounds(LatLngBounds, int, int, int) which allows you to specify the map's dimensions.
05-30 22:19:28.894: E/AndroidRuntime(5593): at kbh.b(Unknown Source)
05-30 22:19:28.894: E/AndroidRuntime(5593): at mas.a(Unknown Source)
05-30 22:19:28.894: E/AndroidRuntime(5593): at mal.a(Unknown Source)
05-30 22:19:28.894: E/AndroidRuntime(5593): at mbi.a(Unknown Source)
05-30 22:19:28.894: E/AndroidRuntime(5593): at fms.onTransact(SourceFile:83)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.os.Binder.transact(Binder.java:310)
05-30 22:19:28.894: E/AndroidRuntime(5593): at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.moveCamera(Unknown Source)
05-30 22:19:28.894: E/AndroidRuntime(5593): at com.google.android.gms.maps.GoogleMap.moveCamera(Unknown Source)
05-30 22:19:28.894: E/AndroidRuntime(5593): at com.bridge.bridgeinventory.MainActivity.getBridgeData(MainActivity.java:148)
05-30 22:19:28.894: E/AndroidRuntime(5593): at com.bridge.bridgeinventory.MainActivity.onCreate(MainActivity.java:44)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1719)
05-30 22:19:28.894: E/AndroidRuntime(5593): ... 11 more
How can I make it work?
The problem is that the map is not loaded yet to the layout that is why you got that error
Solution:
map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
#Override
public void onMapLoaded() {
map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 15));
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}
});
It will call that method when the map is already loaded to the layout.
Related
I implemented the Google Maps tutorial from this link. But I'm getting a runtime error and the app crashes. Please help me.I followed other similar questions but haven't been able to find a solution. The issue seems to be after I updated Google Play Services.
Main Activity:
package com.example.sahayog;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends Activity {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
private GoogleMap map;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Logcat:
03-06 12:21:26.660: E/AndroidRuntime(12319): FATAL EXCEPTION: main
03-06 12:21:26.660: E/AndroidRuntime(12319): Process: com.example.sahayog, PID: 12319
03-06 12:21:26.660: E/AndroidRuntime(12319): java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/maps/model/LatLng;
03-06 12:21:26.660: E/AndroidRuntime(12319): at com.example.sahayog.MainActivity.<clinit>(MainActivity.java:16)
03-06 12:21:26.660: E/AndroidRuntime(12319): at java.lang.reflect.Constructor.newInstance(Native Method)
03-06 12:21:26.660: E/AndroidRuntime(12319): at java.lang.Class.newInstance(Class.java:1572)
03-06 12:21:26.660: E/AndroidRuntime(12319): at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
03-06 12:21:26.660: E/AndroidRuntime(12319): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
03-06 12:21:26.660: E/AndroidRuntime(12319): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
03-06 12:21:26.660: E/AndroidRuntime(12319): at android.app.ActivityThread.access$800(ActivityThread.java:144)
03-06 12:21:26.660: E/AndroidRuntime(12319): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
03-06 12:21:26.660: E/AndroidRuntime(12319): at android.os.Handler.dispatchMessage(Handler.java:102)
03-06 12:21:26.660: E/AndroidRuntime(12319): at android.os.Looper.loop(Looper.java:135)
03-06 12:21:26.660: E/AndroidRuntime(12319): at android.app.ActivityThread.main(ActivityThread.java:5221)
03-06 12:21:26.660: E/AndroidRuntime(12319): at java.lang.reflect.Method.invoke(Native Method)
03-06 12:21:26.660: E/AndroidRuntime(12319): at java.lang.reflect.Method.invoke(Method.java:372)
03-06 12:21:26.660: E/AndroidRuntime(12319): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
03-06 12:21:26.660: E/AndroidRuntime(12319): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-06 12:21:26.660: E/AndroidRuntime(12319): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.model.LatLng" on path: DexPathList[[zip file "/data/app/com.example.sahayog-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
03-06 12:21:26.660: E/AndroidRuntime(12319): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
03-06 12:21:26.660: E/AndroidRuntime(12319): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
03-06 12:21:26.660: E/AndroidRuntime(12319): at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
03-06 12:21:26.660: E/AndroidRuntime(12319): ... 15 more
03-06 12:21:26.660: E/AndroidRuntime(12319): Suppressed: java.lang.ClassNotFoundException: com.google.android.gms.maps.model.LatLng
03-06 12:21:26.660: E/AndroidRuntime(12319): at java.lang.Class.classForName(Native Method)
03-06 12:21:26.660: E/AndroidRuntime(12319): at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
03-06 12:21:26.660: E/AndroidRuntime(12319): at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
03-06 12:21:26.660: E/AndroidRuntime(12319): at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
03-06 12:21:26.660: E/AndroidRuntime(12319): ... 16 more
03-06 12:21:26.660: E/AndroidRuntime(12319): Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
Try this:
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
if(status!=ConnectionResult.SUCCESS)
{
// Google Play Services are not available
Toast.makeText(mContext, "Please install a maps application", Toast.LENGTH_LONG).show();
}
else
{
// Google Play Services are available
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
I have a static List in a model class:
public static List<HomePageOptions> homePageOptions = Arrays.asList(
new HomePageOptions("Title1_1", "Title2_1"),
new HomePageOptions("Title1_2!", "Title2_2"),
new HomePageOptions("Title1_3", "Title2_3"),
new HomePageOptions("Title1_4", "Title2_4")
);
where HomePageOptions is defined:
public class HomePageOptions
{
String Title1;
String Title2;
public HomePageOptions (String title1, String title2)
{
setTitle1(title1);
setTitle2(title2);
}
//regular setters and getters
}
I have an activity that opens as such:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
List<String> values = new ArrayList<String>();
for(HomePageOptions ho : PharmacyModel.homePageOptions){
values.add(ho.getTitle1());
}
}
The for loop is giving me an ExceptionInInitializationError. I come across no problems when i create a regular List right before the for loop, but I would like to keep this structure in the model class. I've been trying to find a solution as to why this is. My guess is the static modifier on the List homePageOptions. Can anyone help?
Here's what the debugger is saying
Thread [<1> main] (Suspended (exception ExceptionInInitializerError))
<VM does not provide monitor information>
HomeActivity.onCreate(Bundle) line: 33
HomeActivity(Activity).performCreate(Bundle) line: 4465
Instrumentation.callActivityOnCreate(Activity, Bundle) line: 1049
ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1920
ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1981
ActivityThread.access$600(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 123
ActivityThread$H.handleMessage(Message) line: 1147
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 137
ActivityThread.main(String[]) line: 4424
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 511
ZygoteInit$MethodAndArgsCaller.run() line: 784
ZygoteInit.main(String[]) line: 551
NativeStart.main(String[]) line: not available [native method]
and LogCat
06-20 04:08:06.274: E/AndroidRuntime(1580): FATAL EXCEPTION: main
06-20 04:08:06.274: E/AndroidRuntime(1580): java.lang.ExceptionInInitializerError
06-20 04:08:06.274: E/AndroidRuntime(1580): at com.allgoodpeopleus.rootsoflife.HomeActivity.onCreate(HomeActivity.java:24)
06-20 04:08:06.274: E/AndroidRuntime(1580): at android.app.Activity.performCreate(Activity.java:4465)
06-20 04:08:06.274: E/AndroidRuntime(1580): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-20 04:08:06.274: E/AndroidRuntime(1580): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-20 04:08:06.274: E/AndroidRuntime(1580): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-20 04:08:06.274: E/AndroidRuntime(1580): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-20 04:08:06.274: E/AndroidRuntime(1580): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-20 04:08:06.274: E/AndroidRuntime(1580): at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 04:08:06.274: E/AndroidRuntime(1580): at android.os.Looper.loop(Looper.java:137)
06-20 04:08:06.274: E/AndroidRuntime(1580): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-20 04:08:06.274: E/AndroidRuntime(1580): at java.lang.reflect.Method.invokeNative(Native Method)
06-20 04:08:06.274: E/AndroidRuntime(1580): at java.lang.reflect.Method.invoke(Method.java:511)
06-20 04:08:06.274: E/AndroidRuntime(1580): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-20 04:08:06.274: E/AndroidRuntime(1580): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-20 04:08:06.274: E/AndroidRuntime(1580): at dalvik.system.NativeStart.main(Native Method)
06-20 04:08:06.274: E/AndroidRuntime(1580): Caused by: java.lang.NullPointerException
06-20 04:08:06.274: E/AndroidRuntime(1580): at ROLModel.Parameter.setValue(Parameter.java:91)
06-20 04:08:06.274: E/AndroidRuntime(1580): at ROLModel.Parameter.<init>(Parameter.java:21)
06-20 04:08:06.274: E/AndroidRuntime(1580): at ROLModel.PharmacyModel.<clinit>(PharmacyModel.java:77)
06-20 04:08:06.274: E/AndroidRuntime(1580): ... 15 more
this
public HomePageOptions (String title1, String title2)
{
setTitle1(title1);
setTitle2(title2);
}
should be
public HomePageOptions (String title1, String title2) {
Title1 = title1;
Title2 = title2;
}
The class PharmacyModel contains the public static List homePageOptions. Elsewhere in the class, I have several instances of the class Parameter:
public static Parameter Age = new Parameter("Age", 30, "years");
where
public class Parameter {
public Object Value;
public Parameter(String name, Object defaultValue, String units) {
setValue(defaultValue);
//set others
}
ParameterChangedEvent listener;
public void addListener(ParameterChangedEvent event){
listener = event;
}
public void setValue(Object value) {
Value = value;
listener.ValueChanged();
}
Because calling setValue in the constructor, listener is null causing the problem. The error manifested itself in the List, but nothing was wrong with that List or HomePageOptions. The solution is to change to line
listener.ValueChanged();
to
if(listener != null) listener.ValueChanged();
I having a problem in passing the intent. The code is written in proper manner still it is showing errors in the logcat. Any help will be greatfull. Thank you
Have i placed the intent code in the write area???
Here is my main file.
MainActivity.java
public class MainActivity extends Activity {
ProgressBar progressBar;
int progressStatus = 0;
TextView textView1, textView2;
Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
textView2 = (TextView) findViewById(R.id.load_per);
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
textView2.setText(progressStatus + "%");
if (progressStatus == 100) {
Intent i = new Intent(MainActivity.this,
EventActivity.class);
startActivity(i);
}
}
});
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
The logcat is showing errors like this
05-30 13:39:51.296: E/AndroidRuntime(1352): FATAL EXCEPTION: main
05-30 13:39:51.296: E/AndroidRuntime(1352): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.temp9/com.example.temp9.EventActivity}: java.lang.NullPointerException
05-30 13:39:51.296: E/AndroidRuntime(1352): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
05-30 13:39:51.296: E/AndroidRuntime(1352): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
05-30 13:39:51.296: E/AndroidRuntime(1352): at android.app.ActivityThread.access$600(ActivityThread.java:122)
05-30 13:39:51.296: E/AndroidRuntime(1352): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
05-30 13:39:51.296: E/AndroidRuntime(1352): at android.os.Handler.dispatchMessage(Handler.java:99)
05-30 13:39:51.296: E/AndroidRuntime(1352): at android.os.Looper.loop(Looper.java:137)
05-30 13:39:51.296: E/AndroidRuntime(1352): at android.app.ActivityThread.main(ActivityThread.java:4340)
05-30 13:39:51.296: E/AndroidRuntime(1352): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 13:39:51.296: E/AndroidRuntime(1352): at java.lang.reflect.Method.invoke(Method.java:511)
05-30 13:39:51.296: E/AndroidRuntime(1352): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-30 13:39:51.296: E/AndroidRuntime(1352): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-30 13:39:51.296: E/AndroidRuntime(1352): at dalvik.system.NativeStart.main(Native Method)
05-30 13:39:51.296: E/AndroidRuntime(1352): Caused by: java.lang.NullPointerException
05-30 13:39:51.296: E/AndroidRuntime(1352): at com.example.temp9.EventActivity.onCreate(EventActivity.java:22)
05-30 13:39:51.296: E/AndroidRuntime(1352): at android.app.Activity.performCreate(Activity.java:4465)
05-30 13:39:51.296: E/AndroidRuntime(1352): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-30 13:39:51.296: E/AndroidRuntime(1352): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
05-30 13:39:51.296: E/AndroidRuntime(1352): ... 11 more
Problem is Caused by: java.lang.NullPointerException
Here com.example.temp9.EventActivity.onCreate(EventActivity.java:22) in EventActivity class' onCreate function at line 22 location you are accessing some null pointer.
Seems like you haven't added EventActivity in AndroidManifest.xml(failed to start activity message in logcat). Make sure you have added the activity in Manifest file.
I am trying to upload image to Facebook using facebook api.
But getting error please find the code below.
I am unable to find out the error. I tried debugging it but am unable to understand the error looking at the log cat. Please help experts. Thanks in advance.
public void postImageonWall(Bitmap bmpImg) {
byte[] data = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmpImg.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
Bundle params = new Bundle();
params.putString("method", "photos.upload");
params.putByteArray("picture", data);
try {
facebook.request(params);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Log Cat:
05-30 05:55:52.489: E/AndroidRuntime(1220): FATAL EXCEPTION: main
05-30 05:55:52.489: E/AndroidRuntime(1220): java.lang.NullPointerException
05-30 05:55:52.489: E/AndroidRuntime(1220): at libcore.net.UriCodec.encode(UriCodec.java:132)
05-30 05:55:52.489: E/AndroidRuntime(1220): at java.net.URLEncoder.encode(URLEncoder.java:50)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.facebook.android.Util.encodeUrl(Util.java:85)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.facebook.android.Util.openUrl(Util.java:145)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.facebook.android.Facebook.request(Facebook.java:717)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.facebook.android.Facebook.request(Facebook.java:633)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.example.dragview.DragActivity.postImageonWall(DragActivity.java:458)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.example.dragview.DragActivity.onOptionsItemSelected(DragActivity.java:428)
05-30 05:55:52.489: E/AndroidRuntime(1220): at android.app.Activity.onMenuItemSelected(Activity.java:2548)
05-30 05:55:52.489: E/AndroidRuntime(1220): at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:372)
05-30 05:55:52.489: E/AndroidRuntime(1220): at android.support.v7.app.ActionBarActivity.superOnMenuItemSelected(ActionBarActivity.java:244)
05-30 05:55:52.489: E/AndroidRuntime(1220): at android.support.v7.app.ActionBarActivityDelegateICS.onMenuItemSelected(ActionBarActivityDelegateICS.java:165)
05-30 05:55:52.489: E/AndroidRuntime(1220): at android.support.v7.app.ActionBarActivity.onMenuItemSelected(ActionBarActivity.java:130)
05-30 05:55:52.489: E/AndroidRuntime(1220): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onMenuItemSelected(ActionBarActivityDelegateICS.java:300)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:547)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
05-30 05:55:52.489: E/AndroidRuntime(1220): at android.view.View.performClick(View.java:4204)
05-30 05:55:52.489: E/AndroidRuntime(1220): at android.view.View$PerformClick.run(View.java:17355)
05-30 05:55:52.489: E/AndroidRuntime(1220): at android.os.Handler.handleCallback(Handler.java:725)
05-30 05:55:52.489: E/AndroidRuntime(1220): at android.os.Handler.dispatchMessage(Handler.java:92)
05-30 05:55:52.489: E/AndroidRuntime(1220): at android.os.Looper.loop(Looper.java:137)
05-30 05:55:52.489: E/AndroidRuntime(1220): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-30 05:55:52.489: E/AndroidRuntime(1220): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 05:55:52.489: E/AndroidRuntime(1220): at java.lang.reflect.Method.invoke(Method.java:511)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-30 05:55:52.489: E/AndroidRuntime(1220): at dalvik.system.NativeStart.main(Native Method)
In Cut the Rope's level clear screen, the score increments up to the value of the score you got in the level. How can I do something like this? I tried using a for loop and just stopped incrementing once I hit the value but this crashes my app. I guess it would need to set the text of the textview on each increment. Any ideas?
Currently this is how I pull the score to display in my Game Over:
// get score and set text field
scorePreferences = getSharedPreferences("score", 0);
score = scorePreferences.getInt("score", 0);
scoreValue.setText(Integer.toString(score));
I'd like it to increment up to value saved in the preferences. I tried to do it like this:
for (int i = 0; i < score; i++) {
scoreValue.setText(i);
}
Log cat messages:
01-30 14:11:19.606: E/AndroidRuntime(26279): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.main.francois/com.main.francois.GameOverActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x0
01-30 14:11:19.606: E/AndroidRuntime(26279): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-30 14:11:19.606: E/AndroidRuntime(26279): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-30 14:11:19.606: E/AndroidRuntime(26279): at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-30 14:11:19.606: E/AndroidRuntime(26279): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-30 14:11:19.606: E/AndroidRuntime(26279): at android.os.Handler.dispatchMessage(Handler.java:102)
01-30 14:11:19.606: E/AndroidRuntime(26279): at android.os.Looper.loop(Looper.java:136)
01-30 14:11:19.606: E/AndroidRuntime(26279): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-30 14:11:19.606: E/AndroidRuntime(26279): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 14:11:19.606: E/AndroidRuntime(26279): at java.lang.reflect.Method.invoke(Method.java:515)
01-30 14:11:19.606: E/AndroidRuntime(26279): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-30 14:11:19.606: E/AndroidRuntime(26279): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-30 14:11:19.606: E/AndroidRuntime(26279): at dalvik.system.NativeStart.main(Native Method)
01-30 14:11:19.606: E/AndroidRuntime(26279): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
01-30 14:11:19.606: E/AndroidRuntime(26279): at android.content.res.Resources.getText(Resources.java:244)
01-30 14:11:19.606: E/AndroidRuntime(26279): at android.widget.TextView.setText(TextView.java:3888)
01-30 14:11:19.606: E/AndroidRuntime(26279): at com.main.francois.GameOverActivity.incrementScore(GameOverActivity.java:135)
01-30 14:11:19.606: E/AndroidRuntime(26279): at com.main.francois.GameOverActivity.load(GameOverActivity.java:130)
01-30 14:11:19.606: E/AndroidRuntime(26279): at com.main.francois.GameOverActivity.onCreate(GameOverActivity.java:78)
01-30 14:11:19.606: E/AndroidRuntime(26279): at android.app.Activity.performCreate(Activity.java:5231)
01-30 14:11:19.606: E/AndroidRuntime(26279): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-30 14:11:19.606: E/AndroidRuntime(26279): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
Edit: I wrote my timer to count the score like this:
public void scoreTimer() {
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
if (incrementScore < score) {
incrementScore++;
scoreValue.setText(String.valueOf(incrementScore));
}
}
}, delay, period);
}
But I now get this error:
01-30 14:57:55.006: E/AndroidRuntime(1383): Process: com.main.francois, PID: 1383
01-30 14:57:55.006: E/AndroidRuntime(1383): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-30 14:57:55.006: E/AndroidRuntime(1383): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6094)
01-30 14:57:55.006: E/AndroidRuntime(1383): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:857)
01-30 14:57:55.006: E/AndroidRuntime(1383): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4320)
01-30 14:57:55.006: E/AndroidRuntime(1383): at android.view.View.invalidate(View.java:10935)
01-30 14:57:55.006: E/AndroidRuntime(1383): at android.view.View.invalidate(View.java:10890)
01-30 14:57:55.006: E/AndroidRuntime(1383): at android.widget.TextView.checkForRelayout(TextView.java:6587)
01-30 14:57:55.006: E/AndroidRuntime(1383): at android.widget.TextView.setText(TextView.java:3813)
01-30 14:57:55.006: E/AndroidRuntime(1383): at android.widget.TextView.setText(TextView.java:3671)
01-30 14:57:55.006: E/AndroidRuntime(1383): at android.widget.TextView.setText(TextView.java:3646)
01-30 14:57:55.006: E/AndroidRuntime(1383): at com.main.francois.GameOverActivity$3.run(GameOverActivity.java:145)
01-30 14:57:55.006: E/AndroidRuntime(1383): at java.util.Timer$TimerImpl.run(Timer.java:284)
I'm guessing I need to use the "asynchronise" or "runonuithread" methods? But I'm not sure how to do this?
Set score as a String
for (int i = 0; i < score; i++) {
scoreValue.setText(String.valueOf(i));
}
Actually when you pass an int to setText() method, it takes it as a resource id and tries to find out the string in string resources (written in res/string.xml). Thats why you are having a ResourceNotFoundException:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.main.francois/com.main.francois.GameOverActivity}:
android.content.res.Resources$NotFoundException: String resource ID #0x0