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;
}
Related
I am using fragment have to Textfield in it. one is for name and other is phone number. Validation for both work just fine. But when I Toast it says null/false.
EditText tPername = (EditText) getView().findViewById(R.id.txtPer);
This is xml
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtPer"
android:layout_marginBottom="15dp"
android:background="#drawable/textbox"
android:layout_gravity="center_horizontal"
android:inputType="textPersonName"/>
When I Toast R.id.txtPer it says null.
This is my java code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_add_customer, container, false);
view.findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
doAdd(view);
}
});
return view;
}
public void doAdd(View view) {
String MobilePattern = "[0-9]{10}";
EditText tPername = (EditText) getView().findViewById(R.id.txtPer);
EditText tMobnum = (EditText) getView().findViewById(R.id.txtMob);
if (TextUtils.isEmpty(tPername.getText().toString())) {
tPername.setError("Please fill this field");
return;
} else if (!tMobnum.getText().toString().matches(MobilePattern)) {
tMobnum.setError("Mobile number must be entered 10 digits only");
return;
}
}
type a (EditText tPername = (EditText) getView().findViewById(R.id.txtPer);)
in the onViewCreated
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
EditText tPername = (EditText) getView().findViewById(R.id.txtPer);
}
String name= tPername.getText().toString();
This solved my problem.Thanks
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;
}
Hello can anybody help me to handle this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at de.kwietzorek.fulcrumwebview.MainActivity$AddServerFragment.onCreateView(MainActivity.java:141)
141 is at btn_back.setOnClickListener(new View.OnClickListener() {
Fragment: AddServerFragement
public class AddServerFragment extends Fragment
implements View.OnClickListener {
#Override
public void onClick(View view) {
}
public AddServerFragment(){
}
Button btn_back, btn_add;
EditText server_ip, server_name;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.add_ip, container, false);
server_ip = (EditText) findViewById(R.id.edit_server_address);
server_name = (EditText) findViewById(R.id.edit_server_name);
btn_back = (Button) findViewById(R.id.btn_back);
btn_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
btn_add = (Button) findViewById(R.id.btn_add);
btn_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String new_server_ip = null, new_server_name = null;
ArrayList<String> server_name_list = new ArrayList<String>();
ArrayList<String> server_ip_list = new ArrayList<String>();
new_server_ip = server_ip.getText().toString();
server_ip_list.add(new_server_ip);
new_server_name = server_name.getText().toString();
server_name_list.add(new_server_name);
}
});
return view;
}
}
Replace
btn_back= (Button) findViewById(R.id.btn_back);
with
btn_back = (Button) view.findViewById(R.id.btn_back);
Do the same for all the other layout elements you wanna initiate.
The reason is: since you inflating a layout file in your Fragment, you have to refer all UI elements to THAT file, that you called "view" here:
View view = inflater.inflate(R.layout.add_ip, container, false);
So the istruction "findViewById" has to be refered to that View view.
You need to find the view inside the view you inflated:
btn_back = (Button) view.findViewById(R.id.btn_back);
I am trying to run an AndroidApp on my phone. I am able to show my welcome fragment, moreover I can trigger the log-message. Unfortunately I am getting a null pointer exception if I want to change the text value from 'welcome' to 'Neuer Text'. What went wrong? I am quite new to android development.
public class WelcomeFragment extends Fragment implements OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
TextView text1 = (TextView) v.findViewById(R.id.welcome_text);
text1.setText("NeuerText");
}
}
In the onClick(), v is the view item that was clicked, e.g. the button, not the view that is inflated in onCreateView().
You should use getView():
public class WelcomeFragment extends Fragment implements OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
switch (v.getId) {
case R.id.button1:
TextView text1 = (TextView) getView().findViewById(R.id.welcome_text);
text1.setText("NeuerText");
break;
}
}
}
Also, if you don't want that action taking place for every button you may want to consider using a switch statement in your onClick().
You cant get the TextView from the button's onClick passed parameter ("View v"), since this is the actual button's view.
You should do something like:
public class WelcomeFragment extends Fragment implements OnClickListener {
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
TextView text1 = (TextView) view.findViewById(R.id.welcome_text);
text1.setText("NewerText"); //Also fixed typo
}
}
In OnClick(View v) , v is the button that you are clicking on. The TextView does not belong to Button, it belongs to R.layout.fragment_welcome. So you find and initialize the TextView inside onCreateView() of the fragment, and then use it inside onClick();
Some what like this:
public class WelcomeFragment extends Fragment implements OnClickListener {
TextView tv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
tv = (TextView) v.findViewById(R.id.welcome_text);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
tv.setText("NeuerText");
}
}
Relatively new to Java and Android programming can anybody help as to why I'm getting a NullPointerException?
public class DpsFragment extends Fragment {
Weapon weppy;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
weppy.setMaxdmg(200);
weppy.setMindmg(100);
TextView tv= (TextView) getView().findViewById(R.id.textView1);
tv.setText("hello");
return inflater.inflate(R.layout.dpsfrag, container, false);
}
public class Weapon {
private int mindmg;
private int maxdmg;
public Weapon(int mindmg, int maxdmg) {
this.setMindmg(mindmg);
this.setMaxdmg(maxdmg);
}
public int getMindmg() {
return mindmg;
}
public void setMindmg(int mindmg) {
this.mindmg = mindmg;
}
public int getMaxdmg() {
return maxdmg;
}
public void setMaxdmg(int maxdmg) {
this.maxdmg = maxdmg;
}
}}
Very simple code, I know, but I have no idea where I have gone wrong? Thanks for any help .
weppy is null so NPE ...i think you forgot to initialize it.
it may be weppy = new Weapon(mindmg,maxdmg);
ALSO
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
weppy = new Weapon(100,200); <<<<<<See here
TextView tv= (TextView) getView().findViewById(R.id.textView1);
tv.setText("hello");
return inflater.inflate(R.layout.dpsfrag, container, false);
}
Also you can not use getView() inside onCreateView because the view you are accessing has not been created yet. So you should do it like this:
View view = inflater.inflate(R.layout.dpsfrag, container, false);
TextView tv= (TextView) view.findViewById(R.id.textView1);
tv.setText("hello");
return view;