As far as I can tell my app looks done and while its not bullet proof it should do the job for testing, but problem is when I build I get these errors.
Thread [<1> main] (Suspended (exception RuntimeException))
ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1573
ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1667
ActivityThread.access$1500(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 117
ActivityThread$H.handleMessage(Message) line: 935
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 3691
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 507
ZygoteInit$MethodAndArgsCaller.run() line: 847
ZygoteInit.main(String[]) line: 605
NativeStart.main(String[]) line: not available [native method]
Here is my code Main.java
package bitcoin.Virwox;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Locale;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
public class Main extends Activity {
//Variables
ProgressDialog dialog = new ProgressDialog(null);
JSONObject jArray = new JSONObject();
int BTCSell;
int GBPBuy;
int Ammount;
int BTCSLLAmmount;
int GBPSLLAmmount;
int Total;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void Start(View view) {
dialog.setTitle("Please Wait");
dialog.setIcon(R.drawable.icon);
dialog.setMessage("Loading...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
Ammount = Integer.valueOf(findViewById(R.id.Amountinputtxt).toString());
GetInfo();
Process();
dialog.hide();
}
public void GetInfo() {
try {
BestBuySell();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final CheckBox checkBox = (CheckBox) findViewById(R.id.VirwoxFeeChk);
if (checkBox.isChecked()) {
ProcessEstimates();
Total = GBPSLLAmmount;
}
else {
int holder;
holder = BTCSell*Ammount;
Total = (int)(holder/GBPBuy);
}
}
private void ProcessEstimates() {
try {
BTCSLL();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
GBPSLL();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void BTCSLL() throws Exception{
//retrieve data for BTC>SLL
try {
HttpClient httpclient = new DefaultHttpClient();
String http = "http://api.virwox.com/api/json.php?method=estimateMarketOrder&amount="+Ammount+"&orderType=SELL&instrument=BTC/SLL";
HttpGet httpget = new HttpGet(http);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
byte buffer[] = new byte[1024] ;
InputStream is = entity.getContent() ;
int numBytes = is.read(buffer) ;
is.close();
String entityContents = new String(buffer,0,numBytes) ;
Log.d("xxx",entityContents);
try {
开发者_Go百科 jArray = new JSONObject(entityContents);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{}
//Parse Data
JSONArray result = jArray.getJSONArray("result");
//parse BTC>SLL
JSONObject f = result.getJSONObject(0);
BTCSLLAmmount = Integer.parseInt(f.getString("amount"));
}
public void GBPSLL() throws JSONException{
//retrieve data for SLL>GBP
try {
HttpClient httpclient = new DefaultHttpClient();
String http = "http://api.virwox.com/api/json.php?method=estimateMarketOrder&amount="+BTCSLLAmmount+"&orderType=BUY&instrument=SLL/GBP";
HttpGet httpget = new HttpGet(http);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
byte buffer[] = new byte[1024] ;
InputStream is = entity.getContent() ;
int numBytes = is.read(buffer) ;
is.close();
String entityContents = new String(buffer,0,numBytes) ;
Log.d("xxx",entityContents);
try {
jArray = new JSONObject(entityContents);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{}
//Parse Data
JSONArray result = jArray.getJSONArray("result");
//parse BTC>SLL
JSONObject e2 = result.getJSONObject(0);
GBPSLLAmmount = Integer.parseInt(e2.getString("amount"));
}
public void Process() {
CheckBox checkBox = (CheckBox) findViewById(R.id.PaypalFeeChk);
if (checkBox.isChecked()) {
Total = Total - 1;
}
TextView tv = (TextView)findViewById(R.id.Outputtxt);
tv.setText("Total: £" + Total + ".00");
CurrentRate();
}
public void CurrentRate(){
float holder;
holder = BTCSell/GBPBuy;
BigDecimal payment = new BigDecimal(holder);
NumberFormat n = NumberFormat.getCurrencyInstance(Locale.UK);
double doublePayment = payment.doubleValue();
String s = n.format(doublePayment);
TextView tv = (TextView)findViewById(R.id.CurrentRatetxt);
tv.setText(s);
}
public void BestBuySell() throws Exception, IOException{
//retrieve data
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://api.virwox.com/api/json.php?method=getBestPrices&symbols[0]=BTC/SLL&symbols[1]=GBP/SLL");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
byte buffer[] = new byte[1024] ;
InputStream is = entity.getContent() ;
int numBytes = is.read(buffer) ;
is.close();
String entityContents = new String(buffer,0,numBytes) ;
Log.d("xxx",entityContents);
try {
jArray = new JSONObject(entityContents);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{}
}
finally{}
//parse info
JSONArray result = jArray.getJSONArray("result");
//parse BTC>SLL
JSONObject e = result.getJSONObject(0);
BTCSell = Integer.parseInt(e.getString("bestBuyPrice"));
//Parse SLL>GBP
e = result.getJSONObject(1);
GBPBuy = Integer.parseInt(e.getString("bestSellPrice"));
}
}
Here's the Layout Main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout android:id="@+id/widget43" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:text="BTC->GBP" android:textSize="45sp" android:layout_width="fill_parent" android:layout_height="78px" android:layout_x="1dip" android:layout_y="20dip" android:gravity="center_horizontal" android:id="@+id/Title"></TextView>
<Button android:layout_width="350px" android:layout_height="wrap_content" android:layout_x="42dip" android:layout_y="298dip" android:text="Calculate" android:id="@+id/Button"></Button>
<TextView android:textSize="15sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="14dip" android:layout_y="388dip" android:text="Current Exchange rate: " android:id="@+id/Ratetxt"></TextView>
<CheckBox android:checked="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_y="148dip" android:layout_x="190dip" android:text="Paypal Fee" android:id="@+id/PaypalFeeChk"></CheckBox>
<CheckBox android:checked="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="190dip" android:layout_y="106dip" android:text="Virwox Fees" android:id="@+id/VirwoxFeeChk"></CheckBox>
<TextView android:layout_x="182dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_y="388dip" android:textSize="15sp" android:id="@+id/CurrentRatetxt" android:text="£0.00"></TextView>
<TextView android:layout_height="wrap_content" android:layout_x="1dip" android:layout_y="245dip" android:gravity="center" android:textSize="18sp" android:id="@+id/Outputtxt" android:text="£0.00" android:layout_width="fill_parent"></TextView>
<EditText android:text="EditText" android:layout_width="200px" android:layout_height="wrap_content" android:layout_x="48dip" android:layout_y="126dip" android:numeric="integer" android:id="@+id/Amountinputtxt"></EditText>
</AbsoluteLayout>
How is this caused and how can I solve this?
Try to declare Context variable before onCreate method:
Context thisContext = this;
@Override
public void onCreate(Bundle savedInstanceState) {
Then when you declare:
AlertDialog.Builder builder = new AlertDialog.Builder(thisContext);
Use the context variable.
Don't use:
AlertDialog.Builder(this);
I'm suspecting it's caused by this line;
ProgressDialog dialog = new ProgressDialog(null);
And I would recommend to move initiating it to e.g onCreate(...)
instead;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dialog = new ProgressDialog(this);
}
精彩评论