Hi, so currently what i am attempting to create a currency convertor app by referencing what other people did such as the example here
android-studio-with-the-use-of-api-38d07e8253fc">https://medium.com/@kanchanaj/how-to-create-a-simple-currency-converter-app-in-android-studio-with-the-use-of-api-38d07e8253fc[
^]
but the issue that i am currently facing now is that i am unsure of the part on the MainActivity as to where and how i should be placing the code for the MainActivity as seen in the link. Should i be placing it in the MainActivity as seen in the link above or should i be placing it inside the fragment that i have created? and if so how should it be placed inside the fragment? Here is an example of my fragment
1 package sg.edu.rp.c346.new4thappcurrencyconvertorpd;
2
3
4 import android.os.Bundle;
5
6 import androidx.fragment.app.Fragment;
7
8 import android.util.Log;
9 import android.view.LayoutInflater;
10 import android.view.View;
11 import android.view.ViewGroup;
12 import android.widget.ArrayAdapter;
13 import android.widget.Spinner;
14 import android.widget.TextView;
15 import android.widget.Toast;
16
17 import com.squareup.okhttp.Callback;
18 import com.squareup.okhttp.OkHttpClient;
19 import com.squareup.okhttp.Request;
20 import com.squareup.okhttp.Response;
21
22 import org.json.JSONException;
23 import org.json.JSONObject;
24
25 import java.io.IOException;
26 import java.text.BreakIterator;
27 import java.util.ArrayList;
28 import java.util.Iterator;
29 import java.util.List;
30
31
32
35 public class HomeFragment extends Fragment implements View.OnClickListener{
36 public static BreakIterator data;
37 List<String> keysList;
38 Spinner toCurrency;
39 TextView textView;
40
41
42 public HomeFragment() {
43
44 }
45
46
47 @Override
48 public View onCreateView(LayoutInflater inflater, ViewGroup container,
49 Bundle savedInstanceState) {
50
51 return inflater.inflate(R.layout.fragment_home, container, false);
52 }
53
54 @Override
55 public void onClick(View v) {
56 btnConvert.setOnClickListener(new View.OnClickListener() {
57 @Override
58 public void onClick(View v) {
59 if(!edtEuroValue.getText().toString().isEmpty())
60 {
61 String toCurr = toCurrency.getSelectedItem().toString();
62 double euroVlaue = Double.valueOf(edtEuroValue.getText().toString());
63
64 Toast.makeText(MainActivity.this, "Please Wait..", Toast.LENGTH_SHORT).show();
65 try {
66 convertCurrency(toCurr, euroVlaue);
67 } catch (IOException e) {
68 e.printStackTrace();
69 Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
70 }
71 }
72 else
73 {
74 Toast.makeText(MainActivity.this, "Please Enter a Value to Convert..", Toast.LENGTH_SHORT).show();
75 }
76
77 }
78 });
79
80 }
81
82 public void loadConvTypes() throws IOException {
83
84 String url = "https://api.exchangeratesapi.io/latest";
85
86 OkHttpClient client = new OkHttpClient();
87
88 Request request = new Request.Builder()
89 .url(url)
90 .header("Content-Type", "application/json")
91 .build();
92
93
94
95 client.newCall(request).enqueue(new Callback() {
96 @Override
97 public void onFailure(Request request, IOException e) {
98 String mMessage = e.getMessage().toString();
99 Log.w("failure Response", mMessage);
100 Toast.makeText(MainActivity.this, mMessage, Toast.LENGTH_SHORT).show();
101 }
102
103 @Override
104 public void onResponse(Response response) throws IOException {
105 final String mMessage = response.body().string();
106
107
108 MainActivity.this.runOnUiThread(new Runnable() {
109 @Override
110 public void run() {
111
112 try {
113 JSONObject obj = new JSONObject(mMessage);
114 JSONObject b = obj.getJSONObject("rates");
115
116 Iterator keysToCopyIterator = b.keys();
117 keysList = new ArrayList<String>();
118
119 while(keysToCopyIterator.hasNext()) {
120
121 String key = (String) keysToCopyIterator.next();
122
123 keysList.add(key);
124
125 }
126
127
128 ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, keysList );
129 toCurrency.setAdapter(spinnerArrayAdapter);
130
131
132
133
134
135 } catch (JSONException e) {
136 e.printStackTrace();
137 }
138
139 }
140 });
141 }
142
143
144
145
146 });
147 }
148
149 public void convertCurrency(final String toCurr, final double euroVlaue) throws IOException {
150
151 String url = "https://api.exchangeratesapi.io/latest";
152
153 OkHttpClient client = new OkHttpClient();
154
155 Request request = new Request.Builder()
156 .url(url)
157 .header("Content-Type", "application/json")
158 .build();
159
160
161
162 client.newCall(request).enqueue(new Callback() {
163 @Override
164 public void onFailure(Request request, IOException e) {
165 String mMessage = e.getMessage().toString();
166 Log.w("failure Response", mMessage);
167 Toast.makeText(HomeFragment.this, mMessage, Toast.LENGTH_SHORT).show();
168 }
169
170 @Override
171 public void onResponse(Response response) throws IOException {
172 final String mMessage = response.body().string();
173 HomeFragment.this.runOnUiThread(new Runnable() {
174 @Override
175 public void run() {
176
177 try {
178 JSONObject obj = new JSONObject(mMessage);
179 JSONObject b = obj.getJSONObject("rates");
180
181 String val = b.getString(toCurr);
182
183 double output = euroVlaue*Double.valueOf(val);
184
185
186 textView.setText(String.valueOf(output));
187
188 } catch (JSONException e) {
189 e.printStackTrace();
190 }
191
192 }
193 });
194 }
195
196
197
198
199
200 });
201 }
202 }
What I have tried:
so what i have currently tried is to follow the instructions as seen in the link above accordingly and placed it in the MainActivity by itself. but however i then came to this issue
Process: sg.edu.rp.c346.new4thappcurrencyconvertorpd, PID: 8656
java.lang.RuntimeException: Unable to start activity ComponentInfo{sg.edu.rp.c346.new4thappcurrencyconvertorpd/sg.edu.rp.c346.new4thappcurrencyconvertorpd.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at sg.edu.rp.c346.new4thappcurrencyconvertorpd.MainActivity.onCreate(MainActivity.java:66)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I'm not very familiar with fragments and am currently still learning android here, so my sincere apologies if my questions seem stupid.