Personality Insight Error - android

FATAL EXCEPTION: main
Process: com.example.dell.ora, PID: 24491
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.squareup.okhttp.Dns$1.lookup(Dns.java:39)
at com.squareup.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:175)
at com.squareup.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:141)
at com.squareup.okhttp.internal.http.RouteSelector.next(RouteSelector.java:83)
at com.squareup.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174)
at com.squareup.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
at com.squareup.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
at com.squareup.okhttp.Call.getResponse(Call.java:286)
at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:243)
at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:205)
at com.squareup.okhttp.Call.execute(Call.java:80)
at com.ibm.watson.developer_cloud.service.WatsonService.execute(WatsonService.java:122)
at com.ibm.watson.developer_cloud.service.WatsonService.executeRequest(WatsonService.java:183)
at com.ibm.watson.developer_cloud.personality_insights.v2.PersonalityInsights.getProfile(PersonalityInsights.java:119)
at com.example.dell.ora.AnalyzerFragment.onCreateView(AnalyzerFragment.java:39)
I am trying to run personality insight from checking the docs I have this in my fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View x = inflater.inflate(R.layout.fragment_analyzer, container, false);
PersonalityInsights service = new PersonalityInsights();
service.setUsernameAndPassword("", "");
service.setEndPoint("https://gateway.watsonplatform.net/personality-insights/api");
EditText content= x.findViewById(R.id.content);
TextView output= x.findViewById(R.id.output);
// String text = content.getText().toString();
String text =getResources().getString(R.string.demo);
Profile profile = service.getProfile(text);
System.out.println(profile);
return x;
}
And to do this I inserted in my gradle file
compile 'com.squareup.okhttp3:okhttp:3.10.0'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.ibm.watson.developer_cloud:java-sdk:2.10.0'
Has anybody encountered this error?Can sb make evident what am I making wrong?Thank You in advance!

new Thread(new Runnable(){Profile profile = service.getProfile(text);
}).start();
Or use an async task. You must only initialize layouts and Views in your onCreateView. All other object initializations must be done in your onActivityCreated
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
final String text =getResources().getString(R.string.demo);
new AsyncTask<Void, Void, Void>() {
#Override
protected Profile doInBackground(Void... params) {
PersonalityInsights service = new PersonalityInsights();
service.setUsernameAndPassword("", "");
service.setEndPoint("https://gateway.watsonplatform.net/personality-insights/api");
Profile profile = service.getProfile(text);
return profile;
}
#Override
protected void onPostExecute(Profile profile) {
super.onPostExecute(profile);
TextView output = x.findViewById(R.id.output);
output.setText(profile.toString())
}
}.execute();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View x = inflater.inflate(R.layout.fragment_analyzer, container, false);
EditText content= x.findViewById(R.id.content);
TextView output = x.findViewById(R.id.output);
// String text = content.getText().toString();
return x;
}

Related

get Editext value in Fragment while inside the Parent Activity method

I have MainActivity in which I call a method to login in a child Fragment called ConnexionFragment. I need to recuperate the email and password from EditTexts located in the Fragment. Can someone help as to how simply explain/do this?
I tried with fragment manager but without success...
MainActivity (Login method)
public void LoginUser(View v){
ConnexionFragment fragment = (ConnexionFragment) manager.findFragmentById(R.id.ConnexionFragment);
courrielStr = fragment.getCourriel();
motDePasseStr = fragment.getMotDePasse();
RequestParams params = new RequestParams();
params.add("type","login");
params.add("courriel", courrielStr);
params.add("mot_de_passe", motDePasseStr);
WebReq.post(context, "index.php", params, new ConnexionResponseHandler());
}
ConnexionFragment
public class ConnexionFragment extends Fragment {
EditText courrielEt, motDePasseEt;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_connexion, container, false);
courrielEt = view.findViewById(R.id.editTextCourrielConnexion);
motDePasseEt = view.findViewById(R.id.editTextPasswordConnexion);
return view;
}
public void onViewCreated(View vue, Bundle savedInstance){
super.onViewCreated(vue,savedInstance);
}
public String getCourriel() {
return courrielEt.getText().toString();
}
public String getMotDePasse() {
return courrielEt.getText().toString();
}
}```

Need Help in Room Database

MyDao Class isn't responding the right way it gives me each time an error , need help to identify problem. Even though it is copies from a youtube class, and i checked the comment if there is something but everybody seems got it right.
public class AddUserFragment extends Fragment {
private EditText UserId,UserName,UserEmail;
private Button Savebn;
public AddUserFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_add_user, container, false);
UserId=view.findViewById(R.id.txt_user_id);
UserName=view.findViewById(R.id.txt_user_name);
UserEmail=view.findViewById(R.id.txt_user_email);
Savebn=view.findViewById(R.id.bn_save);
Savebn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int userid = Integer.parseInt(UserId.getText().toString());
String username = UserName.getText().toString();
String useremail = UserEmail.getText().toString();
User user = new User();
user.setId(userid);
user.setName(username);
user.setEmail(useremail);
MainActivity.myAppDatabase.myDao().addUser(user);
Toast.makeText(getActivity(),"User added successfully",Toast.LENGTH_SHORT).show();
UserId.setText("");
UserName.setText("");
UserEmail.setText("");
}
});
return view;
}
}

Beginner trying out onClickListener in fragment. It keep crashing in runtime

I am new to Java/android develop and trying to follow instructions from android site but cannot find clear answer to implementing onclicklistener in fragment. Please help me why its keep crashing in runtime when I click.
public class TestFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_mngr_user, container, false);
return v;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDBhelper = new UserBaseHelper(getActivity());
// Capture our button from layout
TextView TextviewShow = (TextView)getActivity().findViewById(R.id.text_show);
// Register of onClick listener with the implementation above
TextviewShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something when the text_show is clicked
displayInfo();
}
});
}
.....
}
try this
use this
TextView TextviewShow = (TextView)v.findViewById(R.id.text_show);
instead of this
TextView TextviewShow = (TextView)getActivity().findViewById(R.id.text_show);
change your code as below
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_mngr_user, container, false);
// Capture our button from layout
TextView TextviewShow = (TextView)v.findViewById(R.id.text_show);
// Register of onClick listener with the implementation above
TextviewShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something when the text_show is clicked
displayInfo();
}
});
return v;
}
Try this
public class TestFragment extends Fragment {
View v;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_mngr_user, container, false);
return v;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDBhelper = new UserBaseHelper(getActivity());
// Capture our button from layout
TextView TextviewShow = (TextView)v.findViewById(R.id.text_show);
// Register of onClick listener with the implementation above
TextviewShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something when the text_show is clicked
displayInfo();
}
});
}
.....
}
Move your code from onCreate() method to onCreateView() method and change the following code
From
(TextView)getActivity().findViewById(R.id.text_show);
to
(TextView)v.findViewById(R.id.text_show);

numberformatexception error in fragment

I am New at android stuff, really need help
I tried doing what others said in similar post, but i cant make it work, the error still pop out when i run it after clicking the button if the EditText (modal) field is empty.
I am using fragment in this code, since the other post using activity, i dunno what i miss or what part i am wrong at, please help me...
here is my code
public class Menu_PIAF extends Fragment{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
ScrollView mScrollView = (ScrollView)inflater.inflate(R.layout.piaf_layout,
container, false);
Button button_submit = (Button) mScrollView.findViewById(R.id.button_submit);
button_submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//declare
final EditText modal = (EditText) getView().findViewById(R.id.value_modal);
TextView result_piaf = (TextView) getView().findViewById(R.id.result_piaf);
TextView abc = (TextView) getView().findViewById(R.id.notice);
final String modalx = String.valueOf(modal.getText());
//convert integer
int modaly = new Integer(Integer.parseInt(modalx));
if (modaly >= 5000) {
String piaf = "Deposit";
result_piaf.setText(piaf);
notice.setText("Succeed");
else{
notice.setText("Error");
} }
});
return mScrollView;
}}
Logcat :
Process: com.example.fabio.tabdrawer, PID: 20089
java.lang.NumberFormatException: Invalid int: ""
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parseInt(Integer.java:358)
at java.lang.Integer.parseInt(Integer.java:334)
at com.example.fabio.tabdrawer.Menu_PIAF$1.onClick(Menu_PIAF.java:65)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Try this:
public class Menu_PIAF extends Fragment{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
ScrollView mScrollView = (ScrollView) inflater.inflate(R.layout.piaf_layout, container, false);
EditText modal = (EditText) mScrollView.findViewById(R.id.value_modal);
TextView result_piaf = (TextView) mScrollView.findViewById(R.id.result_piaf);
TextView abc = (TextView) mScrollView.findViewById(R.id.notice);
Button button_submit = (Button) mScrollView.findViewById(R.id.button_submit);
button_submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//declare
String modalx = modal.getText().toString();
if(!modalx.equals("")) {
int modaly = Integer.parseInt(modalx);
if (modaly >= 5000) {
String piaf = "Deposit";
result_piaf.setText(piaf);
notice.setText("Succeed");
} else {
notice.setText("Error");
}
}
}
});
return mScrollView;
}

Doesnt work my text input layout

I'm a beginner in Android :( I'm using a text input layout but I cant catch any value of them! This is my code. For now I want to print the value "usuario" just to check if it has any value. I'm using Butterknife btw D:
public class IngresoFragment extends Fragment {
#Bind(R.id.tilUsuario) TextInputLayout mTilUsuario;
#Bind(R.id.tilPassword) TextInputLayout mTilPassword;
#Bind(R.id.btnIngresar) Button btnIngresar;
#Bind(R.id.tviLibre) TextView tviLibre;
public IngresoFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_ingreso, container, false);
ButterKnife.bind(this,view);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
app();
}
void app(){
mTilUsuario.setHint(getResources().getString(R.string.usuario));
mTilPassword.setHint(getResources().getString(R.string.password));
events();
}
void events(){
btnIngresar.setOnClickListener(new View.OnClickListener() {
String usuario = mTilUsuario.getEditText().getText().toString().trim();
String password = mTilPassword.getEditText().getText().toString().trim();
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), usuario, Toast.LENGTH_LONG).show();
}
});
}
in Android text input box is define as EditText just chenge TextInputLayout to EditText

Categories

Resources