我最近一直试图制作一个从网站上检索信息的应用,但它一直在崩溃。我不确定导致它的原因,但我很确定它与actionPerformed
方法有关。
为什么我的应用程序崩溃?
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.Timer;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class MainActivity extends Activity {
String question;
String url = "https://www.cryptsy.com/markets/view/CLAM_BTC";
TextView price;
Document document = null;
Timer time = new Timer(123, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
document = Jsoup.connect(url).get();
} catch (IOException e1) {
e1.printStackTrace();
}
if(!document.select("#market_price_462").text().equals(question)){
question = document.select("#market_price_462").text();
// System.out.println("Question: " + question);
}
}
});
public void code(){
time.start();
try {
Thread.sleep(2147000000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
price = (TextView) findViewById(R.id.price);
MainActivity asd = new MainActivity();
asd.code();
price.setText(question);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
错误日志: 12-22 22:26:52.709:E / dalvikvm(9554):找不到类' javax.swing.Timer',从方法com.example.clampriceapp.MainActivity中引用。 12-22 22:26:52.709:W / dalvikvm(9554):VFY:无法在Lcom / example / clampriceapp / MainActivity中解析新实例1219(Ljavax / swing / Timer;); 12-22 22:26:52.709:D / dalvikvm(9554):VFY:在0x000a处替换操作码0x22 12-22 22:26:52.709:I / dalvikvm(9554):找不到从方法com.example.clampriceapp.MainActivity.code引用的方法javax.swing.Timer.start 12-22 22:26:52.709:W / dalvikvm(9554):VFY:无法解析虚方法8140:Ljavax / swing / Timer; .start()V 12-22 22:26:52.709:D / dalvikvm(9554):VFY:在0x0002处替换操作码0x6e 12-22 22:26:52.719:I / dalvikvm(9554):解析Lcom / example / clampriceapp / MainActivity $ 1失败;接口1122' Ljava / awt / event / ActionListener;' 12-22 22:26:52.719:W / dalvikvm(9554):课程链接&LIG / example / clampriceapp / MainActivity $ 1;'失败 12-22 22:26:52.719:D / dalvikvm(9554):DexOpt:无法在Lcom / example / clampriceapp / MainActivity中选择直接调用0x1ea4到0x10。 12-22 22:26:52.719:D / dalvikvm(9554):DexOpt:无法选择在Lcom / example / clampriceapp / MainActivity中的0x13处直接调用0x1fcb; 12-22 22:26:52.719:D / AndroidRuntime(9554):关闭VM 12-22 22:26:52.719:W / dalvikvm(9554):threadid = 1:线程退出,未捕获异常(group = 0x417e8da0) 12-22 22:26:52.719:E / AndroidRuntime(9554):致命异常:主要 12-22 22:26:52.719:E / AndroidRuntime(9554):进程:com.example.clampriceapp,PID:9554 12-22 22:26:52.719:E / AndroidRuntime(9554):java.lang.NoClassDefFoundError:javax.swing.Timer 12-22 22:26:52.719:E / AndroidRuntime(9554):at com.example.clampriceapp.MainActivity。(MainActivity.java:31) 12-22 22:26:52.719:E / AndroidRuntime(9554):at java.lang.Class.newInstanceImpl(Native Method) 12-22 22:26:52.719:E / AndroidRuntime(9554):at java.lang.Class.newInstance(Class.java:1208) 12-22 22:26:52.719:E / AndroidRuntime(9554):在android.app.Instrumentation.newActivity(Instrumentation.java:1067) 12-22 22:26:52.719:E / AndroidRuntime(9554):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2299) 12-22 22:26:52.719:E / AndroidRuntime(9554):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495) 12-22 22:26:52.719:E / AndroidRuntime(9554):在android.app.ActivityThread.access $ 900(ActivityThread.java:170) 12-22 22:26:52.719:E / AndroidRuntime(9554):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1304) 12-22 22:26:52.719:E / AndroidRuntime(9554):在android.os.Handler.dispatchMessage(Handler.java:102) 12-22 22:26:52.719:E / AndroidRuntime(9554):在android.os.Looper.loop(Looper.java:146) 12-22 22:26:52.719:E / AndroidRuntime(9554):在android.app.ActivityThread.main(ActivityThread.java:5635) 12-22 22:26:52.719:E / AndroidRuntime(9554):at java.lang.reflect.Method.invokeNative(Native Method) 12-22 22:26:52.719:E / AndroidRuntime(9554):at java.lang.reflect.Method.invoke(Method.java:515) 12-22 22:26:52.719:E / AndroidRuntime(9554):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1291) 12-22 22:26:52.719:E / AndroidRuntime(9554):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) 12-22 22:26:52.719:E / AndroidRuntime(9554):at dalvik.system.NativeStart.main(Native Method)