I'm new to android programming and I'm having a problem with my codes. Can anyone help me or point out the cause of my error because I'm not really sure why it's giving me a NullPointerException when its a text view or if that is possible.
LogCat:
java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference
at com.example.app.rosbridge.MainActivity$2$1.run(MainActivity.java:133)
Here is the code for that line:
current.setText(String.format("%.4f%s", batteryStateData.msg.current * Math.pow(10, 6), "A"));
But when i run my app my voltage is setting null and here is the code for the voltage:
voltage.setText(String.format("%.4f%s", batteryStateData.msg.voltage, "v"));
Here is the full code:
public class MainActivity extends Activity {
private TextView voltage, current, percentage, status;
private SubscribedData<BatteryState> batteryStateData;
private RosbridgeListener rosbridge;
private boolean subscribed = false;
private boolean advertised = false;
/** Indicates that Lint should ignore the specified warnings for the annotated element. */
#SuppressLint("ClickableViewAccessibility")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity);
Button settings_btn = (Button) findViewById(R.id.connect_btn);
voltage = findViewById(R.id.voltage_txt);
current = findViewById(R.id.current_txt);
percentage = findViewById(R.id.percentage_txt);
status = findViewById(R.id.status_txt);
connectButton = findViewById(R.id.connect_btn);
batteryStateData = new SubscribedData<>();
final Type batteryStateType = new TypeToken<SubscribedData<BatteryState>>() {
}.getType();
// ROSBRIDGE protocol allows access to underlying ROS messages and services as serialized JavaScript Object Notation (JSON) objects
WebSocket protocol communicates to a server for the connection from a user's web browser
//A connection to the rosbridge thru the IP address of the robot from the socket
rosbridge = new RosbridgeListener("ws://10.24.204.231:9090");
rosbridge.setOnDataReceivedListener(new RosbridgeMessageListener() {
// a running thread that when the connection is made the data of the topic will serialize and deserialized java objects to (and from) JSON. #param msg
#Override
public void onDataReceived(final String msg) {
try {
runOnUiThread(new Runnable() {
#Override
public void run() {
batteryStateData = new Gson().fromJson(msg, batteryStateType);
voltage.setText(String.format("%.4f%s", batteryStateData.msg.voltage, "v"));
current.setText(String.format("%.4f%s", batteryStateData.msg.current * Math.pow(10, 6), "A"));
percentage.setText(String.format("%.2f%s", batteryStateData.msg.percentage, "%"));
status.setText(String.format("%s", PowerSupplyStatus.values()[batteryStateData.msg.powerSupplyStatus]));
}
});
Log.d("B9T", String.format("Received data: %s", msg));
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
connectButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!subscribed) {
rosbridge.Subscribe("/battery", "sensor_msgs/BatteryState");
subscribed = true;
connectButton.setText("Disconnect");
} else {
rosbridge.UnSubscribe("/battery");
subscribed = false;
connectButton.setText("Connect");
}
}
});
Everytime when you are setting the text to textview, you must need to check if it is not null.
You can check null before you set text on textview like the following.
#Override
public void run() {
batteryStateData = new Gson().fromJson(msg, batteryStateType);
// check null before set text or calculate something
if(batteryStateData.msg.current != null){
current.setText(String.format("%.4f%s", batteryStateData.msg.current * Math.pow(10, 6), "A"));
}
// you can check belows like above
voltage.setText(String.format("%.4f%s", batteryStateData.msg.voltage, "v"));
percentage.setText(String.format("%.2f%s", batteryStateData.msg.percentage, "%"));
status.setText(String.format("%s", PowerSupplyStatus.values()[batteryStateData.msg.powerSupplyStatus]));
}
Check weather your created class with GSON is not null
and then check class fields values is not null or 0.
batteryStateData = new Gson().fromJson(msg, batteryStateType);
if (batteryStateData != null) {
if (batteryStateData.msg.voltage!=0)
voltage.setText(String.format("%.4f%s", batteryStateData.msg.voltage, "v"));
if (batteryStateData.msg.current!=0)
current.setText(String.format("%.4f%s", batteryStateData.msg.current * Math.pow(10, 6), "A"));
if (batteryStateData.msg.percentage!=0)
percentage.setText(String.format("%.2f%s", batteryStateData.msg.percentage, "%"));
if (batteryStateData.msg.values !=null)
status.setText(String.format("%s", PowerSupplyStatus.values()[batteryStateData.msg.powerSupplyStatus]));
}
Related
Am trying to implement Braintree Vault PayPal payment, the problem am facing here is getting the paymentMethodNonce my event listener createdListener to capture nonce doesn't get called using vault, but everything works fine using checkout. I can't charge customer without a paymentMethodNonce, please can anyone assist me.
mBraintreeFragment = BraintreeFragment.newInstance(this,"TOKEN_FROM_SERVER");
PayPalRequest request = new PayPalRequest().localeCode("US").billingAgreementDescription("Your agreement description");
PayPal.requestBillingAgreement(mBraintreeFragment, request);
mBraintreeFragment.addListener(createdListener);
mBraintreeFragment.addListener(cancelListener);
mBraintreeFragment.addListener(errorListener);
DataCollector.collectDeviceData(mBraintreeFragment, new BraintreeResponseListener<String>() {
#Override
public void onResponse(String deviceData) {
Log.e("PayPal", deviceData);
try {
JSONObject json = new JSONObject(deviceData);
deviceDataInfo = json.getString("correlation_id");
Log.e("PayPal", deviceDataInfo);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
My Listeners
PaymentMethodNonceCreatedListener createdListener = new PaymentMethodNonceCreatedListener() {
#Override
public void onPaymentMethodNonceCreated(PaymentMethodNonce paymentMethodNonce) {
String nonce = paymentMethodNonce.getNonce();
Log.d("PayPal", "nonce id " + nonce);
}
};
BraintreeCancelListener cancelListener = new BraintreeCancelListener() {
#Override
public void onCancel(int requestCode) {
Log.d("CreditCard", "Braintree Error Code " + requestCode);
}
};
BraintreeErrorListener errorListener = new BraintreeErrorListener() {
#Override
public void onError(Exception error) {
if (error instanceof ErrorWithResponse) {
ErrorWithResponse errorWithResponse = (ErrorWithResponse) error;
BraintreeError cardErrors = errorWithResponse.errorFor("creditCard");
if (cardErrors != null) {
List<BraintreeError> errors = cardErrors.getFieldErrors();
String err = Objects.requireNonNull(errors.get(0).getMessage());
Log.d("CreditCard", errors.toString());
}
}
}
};
Instead of adding manually your listeners to that request, it's better to just implement the interface from braintree.
For example, if you want to use the onPaymentMethodNonceCreated() just add "implements PaymentMethodNonceCreatedListener" after your class name.
public class "YourClass" implements PaymentMethodNonceCreatedListener {
//...
}
And then override the method that now the Android Studio is warning you:
#Override
public void onPaymentMethodNonceCreated(PaymentMethodNonce paymentMethodNonce) {
String nonce = paymentMethodNonce.getNonce();
//...
}
This way you can go for everyone of your listeners! Good luck!
In a service I'm populating an ArrayList, which I then return to the calling activity:
In service (here, resultArrayList contains items and is of class ArrayList<MyObjs>):
public class DataFetchService extends BaseService {
#Override
protected void onHandleIntent(final Intent intent) {
super.onHandleIntent(intent);
// Do some work here that populates resultArrayList...
final Bundle bundle = new Bundle();
bundle.putSerializable(BaseService.RESULT_OBJ, resultArrayList);
message.setData(bundle);
try {
final Messenger messenger = startIntent.getParcelableExtra(BaseService.PARAM_MESSENGER);
messenger.send(message);
} catch (RemoteException e) {
L.p("Help!");
}
From BaseService:
public class BaseService extends IntentService {
protected ArrayList<MyObjs> resultArrayList = new ArrayList<>();
// Yada yada...
In the activity's handleMessage():
#Override
public boolean handleMessage(final Message msg) {
final Bundle bundle = msg.getData();
#SuppressWarnings("unchecked")
final ArrayList<MyObjs> nodes = (ArrayList<MyObjs>) bundle.getSerializable(BaseService.RESULT_OBJ);
if (nodes == null) {
//App never enters this
return
}
if (nodes.size() == 0) {
// Always enters here!
// If I set a breakpoint here, the IDE tells me nodes size is 1
}
The weird thing is that if I set a breakpoint inside the if (nodes.size == 0) { code, the IDE shows that nodes does contain items (says size = 1 and I can expand it and see the variables), even though it enters that.
Any idea what could be the issue? Could this be a race condition between other services sending data back to handleMessage()?
I'm uncertain about the causes of this error, but creating a new ArrayList<> before sending from the service seems to fix it.
So the following makes it work:
final Bundle bundle = new Bundle();
// Created this new ArrayList
final ArrayList<MyObjs> newArrayList = new ArrayList<>();
// Add items to the new ArrayList
newArrayList.addAll(resultArrayList);
// Send the new ArrayList and NOT the other one.
bundle.putSerializable(BaseService.RESULT_OBJ, newArrayList);
message.setData(bundle);
try {
final Messenger messenger = startIntent.getParcelableExtra(BaseService.PARAM_MESSENGER);
messenger.send(message);
} catch (RemoteException e) {
L.p("Help!");
}
I'm trying to retrieve some data from API, but i'm always getting null in async task. Here is my asynctask:
private class DownloadTask extends AsyncTask<Bundle, Void, List<Topic>> {
#Override
protected void onPreExecute() {
HomeActivity.mProgressBar.setVisibility(View.VISIBLE);
HomeActivity.mProgressBar.setIndeterminate(true);
}
#Override
protected List<Topic> doInBackground(Bundle... params) {
return downloadPhotos(params[0]);
}
#Override
protected void onPostExecute(List<Topic> topics) {
HomeActivity.mProgressBar.setVisibility(View.INVISIBLE);
HomeActivity.mProgressBar.setIndeterminate(false);
Log.d("List Size: ", ""+topics); // 0
adapter = new TopicListAdapter(activity, topics);
RecyclerView.LayoutManager manager = new MyCustomLayoutManager(activity);
recyclerView.setLayoutManager(manager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
}
}
Method for retrieving data should merge two arrays into one array because i'm retrieving data from two places:
private List<Topic> downloadPhotos(Bundle params) {
String profileId = activity.getPreferencesManager().getProfileId();
List<Topic> topicsFromMe, topicsFromFriends;
topicsFromFriends = setValuesFromFriends(params);
topicsFromMe = setValuesFromMe(profileId, params);
topicsFromFriends.addAll(topicsFromMe);
sortTopics(topicsFromFriends);
int k = topicsFromFriends.size();
Log.d("List Size: ", "" + topicsFromFriends); // here also 0 for size
if (k > 10)
topicsFromFriends.subList(10, k).clear();
return topicsFromFriends;
}
And here is one method where i'm setting values to array list. It is strange that RecyclerView in this case is populated with this array, but i'm not getting results i want. For instance i should sort this list and show only 10 records from it.
private List<Topic> setValuesFromFriends(final Bundle params) {
final List<Topic> topics = new ArrayList<>();
activity.getSimpleFacebook().getFriends(new OnFriendsListener() {
#Override
public void onComplete(List<Profile> friends) {
for (final Profile profile : friends) {
activity.getSimpleFacebook().get(profile.getId(), "photos/uploaded", params,
new OnActionListener<List<Photo>>() {
#Override
public void onComplete(List<Photo> photos) {
for (final Photo photo : photos) {
// Initialize instance of Topic
final User user = photo.getFrom();
final Topic topic = new Topic();
topic.setCaption(photo.getName());
topic.setImageId(photo.getId());
topic.setCreatedTime(photo.getCreatedTime());
topic.setPostImage(photo.getSource());
topic.setUserId(user.getId());
topic.setName(user.getName());
final Bundle likeParams = new Bundle();
likeParams.putString("fields", "total_count");
likeParams.putString("limit", "100000");
activity.getSimpleFacebook().get(photo.getId(), "likes",
likeParams, new OnActionListener<List<Like>>() {
#Override
public void onComplete(List<Like> likes) {
topic.setNumOfLikes(likes.size());
topics.add(topic);
}
#Override
public void onThinking() {
super.onThinking();
}
});
}
}
});
}
}
});
return topics;
}
You are using AsyncTask incorrectly.
AsyncTask is launching another Thread (thread1) where where it is executing the method, doenloadPhotos. This method is calling setValuesFromFriends which is creating another thread (thread2) with the method getFriends. As thread2 has been launched, the rest of the code in setValuesFromFriends will get executed.
So here is how it is working:
private List<Topic> setValuesFromFriends(final Bundle params) {
final List<Topic> topics = new ArrayList<>();
//launched process on new thread
return topics; //this is 0 as topics = new ArrayList<>();
}
So now topicsFromFriends = 0. Hence you are getting the output = 0.
in effect thread1 is getting executed before thread2 is complete. As the output of thread1 is 0, nothing is displayed in UI after onPostExecute
There is no need of using AsyncTask.
You should put all the required code inside the onComplete of the new OnFriendsListener(). This way the info will be shown correctly. You can launch the progressbar before setValuesFromFriends and then remove it in the onComplete.
I am working on developer authenticated project.And I am trying to get credentials using this below code.But it is giving me following error.
I have pasted IdentityId and Token manually into this code from server code:
Caused by: com.amazonaws.services.cognitoidentity.model.ResourceNotFoundException: Identity 'ap-northeast-1:fe81cd76-e9d4-4416-99ea-b684b78743c8' not found. (Service: AmazonCognitoIdentity; Status Code: 400; Error Code: ResourceNotFoundException; Request ID: 2ac65fe8-d41a-11e5-8674-677eefdb5331)
at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:709)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:385)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:196)
at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.invoke(AmazonCognitoIdentityClient.java:533)
at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.getCredentialsForIdentity(AmazonCognitoIdentityClient.java:406)
at com.example.sandesh.aws.MainActivity$network.doInBackground(MainActivity.java:101)
at com.example.sandesh.aws.MainActivity$network.doInBackground(MainActivity.java:52)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
Here is my code: MainActivity.java
public class MainActivity extends AppCompatActivity {
Button button;
protected static CognitoCachingCredentialsProvider credentialsProvider = null;
private GetCredentialsForIdentityResult credentialsForIdentityResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
network net = new network();
net.execute();
}
});
}
public class network extends AsyncTask<Void,Void,String>{
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected String doInBackground(Void... params) {
Authentication developerProvider = new Authentication(
null,
"ap-northeast-1:XXXXXXXXXXXXXXXXXX",
Regions.AP_NORTHEAST_1);
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
getApplicationContext(),
developerProvider,
Regions.AP_NORTHEAST_1);
HashMap<String, String> loginsMap = new HashMap<String, String>();
loginsMap.put(developerProvider.getProviderName(), "7386372772");
credentialsProvider.setLogins(loginsMap);
credentialsProvider.refresh();
GetCredentialsForIdentityRequest credentialsForIdentityRequest = new GetCredentialsForIdentityRequest();
credentialsForIdentityRequest.setIdentityId(developerProvider.getIdentityId());
credentialsForIdentityRequest.setLogins(loginsMap);
AmazonCognitoIdentityClient cognitoIdentityClient = new AmazonCognitoIdentityClient(credentialsProvider);
credentialsForIdentityResult = cognitoIdentityClient.getCredentialsForIdentity(credentialsForIdentityRequest);
Log.d("access_key",credentialsForIdentityResult.getCredentials().getAccessKeyId());
return credentialsForIdentityResult.getCredentials().getAccessKeyId();
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}}
Authentication.java
public class Authentication extends AWSAbstractCognitoDeveloperIdentityProvider {
private static final String PROVIDERNAME = "login.blupinch.app";
public String response = " ";
String line = " ";
public Authentication(String accountId, String identityPoolId, Regions region) {
super(accountId, identityPoolId, region);
}
#Override
public String getProviderName() {
return PROVIDERNAME;
}
public String refresh() {
setToken(null);
if (getProviderName() != null &&
!this.loginsMap.isEmpty() &&
this.loginsMap.containsKey(getProviderName())) {
update(identityId, token);
return "eyJraWQiOiJhcC1ub3J0aGVhc3QtMTEiLCJ0eXAiOiJKV1MiLCJhbGciOiJSUzUxMiJ9.eyJzdWIiOiJhcC1ub3J0aGVhc3QtMTpmZTgxY2Q3Ni1lOWQ0LTQ0MTYtOTllYS1iNjg0Yjc4NzQzYzgiLCJhdWQiOiJhcC1ub3J0aGVhc3QtMTphODcxZmE1Zi0yM2EyLTQ4MGQtYmFhNi1iNGVkMzE0MzcyNDQilCJhbXIiOlsiYXV0aGVudGljYXRlZCIsImxvZ2luLmJsdXBpbmNoLmFwcCIsImxvZ2luLmJsdXBpbmNoLmFwcDphcC1ub3J0aGVhc3QtMTphODcxZmE1Zi0yM2EyLTQ4MGQtYmFhNi1iNGVkMzE0MzcyNDQ6NzM4NjM3Mjg3MiJdLCJpc3MiOiJodHRwczovL2NvZ25pdG8taWRlbnRpdHkuYW1hem9uYXdzLmNvbSIsImV4cCI6MTQ1NTU5NTM5NywiaWF0IjoxNDU1NTU5Mzk3fQ.fHHH6aeCn5EaJGxGD6tw7bWyQpPHuYcW8SZLRGVn-3cbamJrWEPmUnNvcLJ-D6nL8AvMQy7-s1LGQ5MNaiuIH7QF6W8aWt2OMALmA_Y7eqpGQ6iQXVma_jTZSpiyBe2cPNggWgeslPtFxomwE90vW0rzS1sY3D5Y3UbnrIHNdiPKIzzP9JaQo1IsTJMKEpQM-jzWP6stV1radDuIzWQroBVQseOQSD-MXV_-cgWWSx0eQmtFbjJW6RP_nACgh0uTbGmMuOi2iKXKQAdGlYWO-PHlShbiHT-WLQoZNWuh95Hh9dMldv-mNdnYSblqYyqptLA3kObioI08XXkTqwaaAw";
} else {
this.getIdentityId();
return null;
}
}
public String getIdentityId() {
identityId = "ap-northeast-1:XXXXXXXXXXXXXXXXXXXXXXXX";
if (identityId == null) {
if (getProviderName() != null && !this.loginsMap.isEmpty()
&& this.loginsMap.containsKey(getProviderName())) {
update(identityId, token);
return "ap-northeast-1:XXXXXXXXXXXXXXXXXXXX";
} else {
return super.getIdentityId();
}
} else {
return identityId;
}
}}
I have tried it in PHP where I performed getOpenIdTokenForDeveloperIdentity , stsClient and AssumerolewithWebIdentity to get credentials it worked great. Thank you in advance.
When using Developer Authenticated Identities, the logins map for the call GetCredentialsForIdentity should have "cognito-identity.amazonaws.com" as the key instead of developer provider name.
Also, you should not be calling this method yourself, the SDK will take care of this.
Update:
When calling getCredentialsForIdentity, in the logins map, when key is "cognito-identity.amazonaws.com", the value should be the token you got from your backend after calling GetOpenIdTokenForDeveloperIdentity. You do not need to get the credentials for doing AWS operations, just initialize the AWS Service Client with this credentials provider and you should be good to go.
(1) In your application, you do not need to call GetCredentialsForIdentity, the CredentialsProvider does all of this for you. (And ensures the correct arguments are passed in).
--> You can test that you are getting credentials by making a call to another service that requires credentials (ex S3)
--> Another option is to call getCredentials() on the Credentials Provider and validate that an exception is not thrown
(2) In Authentication.java, refresh, you seem to be using a hard coded token? This is likely expired, and you should make sure to get new ones. (If you are just testing app side, make sure you hard code a fresh one).
(3) In Authentication.java, refresh, it looks like you are calling update with a null token.
(4) Also make sure you are getting tokens for the hard coded identity you are using. (Again, you should only be hard coding this if you are testing).
My problem is annoying. My server side is generating 12 random numbers (double here).
My Client side received the correct data but nothing is displayed in my Chart. That worked fine with hardcoded data in the store but not with a REST call.
The transfer between my server and my client is that :
[{"key":"key0","value":0.47222548599297787},{"key":"key1","value":0.6009173797369691},{"key":"key2","value":0.13880104282435624},{"key":"key3","value":0.01804674319345545},{"key":"key4","value":0.5547733564202956},{"key":"key5","value":0.8229999661308851},{"key":"key6","value":0.8959346004391032},{"key":"key7","value":0.6848052288628435},{"key":"key8","value":0.10222856671111813},{"key":"key9","value":0.6931371931409103},{"key":"key10","value":0.2994297934549003},{"key":"key11","value":0.47566752196381334}]
Here my simple class used for my test. I am a newbie with GXT 3
public void onModuleLoad() {
final ListStore<JSOModel> store;
final ContentPanel panel = new FramedPanel();
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, "/ws/DocumentService/v1/test");
builder.setHeader("Accept", "application/json");
HttpProxy proxy = new HttpProxy(builder);
final Loader<ListLoadConfig, ListLoadResult<JSOModel>> loader = new ListLoader<ListLoadConfig, ListLoadResult<JSOModel>>(proxy, new DataReader<ListLoadResult<JSOModel>, String>() {
#Override
public ListLoadResult<JSOModel> read(Object loadConfig, String data) {
List<JSOModel> jsoModels = new ArrayList<JSOModel>();
JsArray<JSOModel> jsoModelJsArray = JSOModel.arrayFromJson(data);
if(jsoModelJsArray != null) {
for(int i = 0; i < jsoModelJsArray.length(); i++) {
jsoModels.add(jsoModelJsArray.get(i));
}
}
return new ListLoadResultBean<JSOModel>(jsoModels);
}
});
store = new ListStore<JSOModel>(new ModelKeyProvider<JSOModel>() {
#Override
public String getKey(JSOModel item) {
return item.get("key");
}
});
loader.addLoadHandler(new LoadResultListStoreBinding<ListLoadConfig, JSOModel, ListLoadResult<JSOModel>>(store) {
#Override
public void onLoad(LoadEvent<ListLoadConfig, ListLoadResult<JSOModel>> event) {
ListLoadResult<JSOModel> loaded = event.getLoadResult();
if(loaded.getData() == null) {
store.replaceAll(new ArrayList<JSOModel>());
} else {
store.replaceAll(loaded.getData());
}
}
});
Chart<JSOModel> chart = new Chart<JSOModel>();
chart.setStore(store);
chart.setShadowChart(true);
NumericAxis<JSOModel> axis = new NumericAxis<JSOModel>();
axis.setPosition(Chart.Position.LEFT);
axis.addField(new ValueProvider<JSOModel, Number>() {
#Override
public Number getValue(JSOModel JSOModel) {
return JSOModel.getNumber("value");
}
#Override
public void setValue(JSOModel JSOModel, Number number) {
}
#Override
public String getPath() {
return "key";
}
});
axis.setTitleConfig(new TextSprite("Number of hits"));
axis.setWidth(50);
axis.setMinimum(0);
axis.setMaximum(100);
chart.addAxis(axis);
PathSprite odd = new PathSprite();
odd.setOpacity(1);
odd.setFill(new Color("#dff"));
odd.setStroke(new Color("#aaa"));
odd.setStrokeWidth(0.5);
axis.setGridOddConfig(odd);
CategoryAxis<JSOModel, String> horizontalAxis = new CategoryAxis<JSOModel, String>();
horizontalAxis.setPosition(Chart.Position.BOTTOM);
horizontalAxis.setField(new ValueProvider<JSOModel, String>() {
#Override
public String getValue(JSOModel JSOModel) {
return JSOModel.get("key");
}
#Override
public void setValue(JSOModel JSOModel, String s) {
}
#Override
public String getPath() {
return "key";
}
});
horizontalAxis.setTitleConfig(new TextSprite("month of year"));
chart.addAxis(horizontalAxis);
LineSeries<JSOModel> column = new LineSeries<JSOModel>();
column.setYAxisPosition(Chart.Position.LEFT);
column.setStroke(new RGB(148,174,10));
column.setHighlighting(true);
chart.addSeries(column);
axis.addField(column.getYField());
chart.addSeries(column);
chart.setHeight(100);
chart.setWidth(100);
Button b = new Button("ha");
b.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent clickEvent) {
loader.load();
}
});
RootPanel.get().add(b);
panel.setCollapsible(true);
panel.setHeadingText("Column Chart");
panel.setPixelSize(620, 500);
panel.setBodyBorder(true);
VerticalLayoutContainer layout = new VerticalLayoutContainer();
panel.add(layout);
chart.setLayoutData(new VerticalLayoutContainer.VerticalLayoutData(1,1));
layout.add(chart);
chart.setBackground(new Color("#dff"));
RootPanel.get().add(panel);
There are two ways to wire the chart into a store. One is to simply specify that the chart is using a store via setStore, as you have done:
chart.setStore(store);
When you do this, you must also inform the chart when it must redraw everything - you must call:
chart.redrawChart();
This call must be made shortly after the load is completed - consider doing it at the end of onLoad.
Why is this required? In some cases, developers want to make many changes to the store, one at a time, and if the chart automatically updated after each change, that would spawn many slow changes to the data model, and could end up looking strange. In a case like this, you would only call redrawChart() after all changes were complete.
There is another option however - instead of calling setStore, you can call bindStore, and ask the Chart to automatically update whenever any change occurs to the chart:
chart.bindStore(store);
In your case, this is likely the correct answer.