Issue
everyone. I'm using drawer activity, changing content using fragments, so there's PerfilFragment, for example:
public class PerfilFragment extends Fragment {
final CurrentView currentView = new CurrentView();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_perfil, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (getActivity() != null) {
getActivity().setTitle(R.string.menu_perfil);
final CognitoCachingCredentialsProvider credentialsProvider;
if (getContext() != null) {
AWSConfiguration awsConfig = new AWSConfiguration(getContext());
credentialsProvider = new CognitoCachingCredentialsProvider(
getContext(),
awsConfig
);
currentView.setCredentialsProvider(credentialsProvider);
currentView.setView(view);
final FloatingActionButton fab = view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Insert().doInBackground(currentView);
}
});
}
}
}
}
The type CurrentView is an internal class, as well as Insert class:
class CurrentView {
private CognitoCachingCredentialsProvider credentialsProvider;
private View view;
CognitoCachingCredentialsProvider getCredentialsProvider() {
return credentialsProvider;
}
void setCredentialsProvider(CognitoCachingCredentialsProvider credentialsProvider) {
this.credentialsProvider = credentialsProvider;
}
View getView() {
return view;
}
void setView(View view) {
this.view = view;
}
}
class Insert extends AsyncTask<CurrentView, Void, Table> {
private final static String TABLE = "perfil";
@Override
protected Table doInBackground(CurrentView... currentView) {
AmazonDynamoDBClient dbClient;
try {
dbClient = new AmazonDynamoDBClient(currentView[0].getCredentialsProvider());
Table perfilTable = Table.loadTable(dbClient, TABLE);
Document perfilDocument = new Document();
final EditText perfilNome = currentView[0].getView().findViewById(R.id.perfil_nome_txt);
final EditText perfilDataNasc = currentView[0].getView().findViewById(R.id.perfil_datanasc_txt);
final EditText perfilGenero = currentView[0].getView().findViewById(R.id.perfil_genero_txt);
perfilDocument.put("userId", currentView[0].getCredentialsProvider().getCachedIdentityId());
perfilDocument.put("nome", perfilNome.getText().toString());
perfilDocument.put("datanasc", perfilDataNasc.getText().toString());
perfilDocument.put("genero", perfilGenero.getText().toString());
Document perfilInserido = perfilTable.putItem(perfilDocument);
if (perfilInserido.containsKey("genero")) {
DynamoDBEntry perfilInseridoGenero = perfilInserido.get("genero");
if (perfilInseridoGenero != null) {
if (perfilInseridoGenero.asString().equals(perfilGenero.getText().toString())) {
Snackbar
.make(
currentView[0].getView(),
"Perfil atualizado!",
Snackbar.LENGTH_LONG
)
.setAction("Atualizar", null)
.show();
}
}
}
return perfilTable;
} catch (Exception e) {
Log.e("EXCEPTION", e.getMessage(), e);
return null;
}
}
}
The exception is thrown inside doInBackground method, at the line:
Table perfilTable = Table.loadTable(dbClient, TABLE);
What am I missing?
Solution
As AWS Developer Forums' user awsvbk answered to me:
You need to invoke the execute() method as opposed to the doInBackground() method. Calling execute() will internally call the doInBackground() method of AsyncTask in a background thread.
new Insert().execute(<PARAMS>)
If you still face any other issue with the DynamoDB Document Model SDK, please cut us an issue on GitHub on https://github.com/aws/aws-sdk-android
Answered By - Rasshu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.