我对编码很新,我有一个问题。好吧我试图创建一个自定义ListView与我用jSoup解析的一些字符串,但由于某种原因它不起作用,它编译,但当它在模拟器上运行时,应用程序崩溃。这是代码和调试器:
package davidgb.baseballspain;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
//272
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button but1 = (Button) findViewById(R.id.but);
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new doit().execute();
}
});}
public class doit extends AsyncTask<Void, Void, Void> {
ListView lista = (ListView)findViewById(R.id.lista);
@Override
protected void onPostExecute(Void aVoid) {
CustomAdapter customAdapter = new CustomAdapter();
lista.setAdapter(customAdapter);}
String result1;
String result2;
String result3;
String result4;
String result5;
String result6;
String result7;
String result8;
String result9;
String result10;
String result11;
String result12;
String result13;
String result14;
String result15;
String result16;
String result17;
String result18;
String result19;
String result20;
String result21;
String result22;
String result23;
String result24;
String result25;
String result26;
String result27;
String result28;
String result29;
String result30;
String result31;
String result32;
String result33;
String result34;
String result35;
String result36;
String result37;
String result38;
String result39;
String result40;
String result41;
String result42;
String result43;
String result44;
String result45;
String result46;
String result47;
String result48;
String result49;
String result50;
String result51;
String result52;
String result53;
String result54;
String result55;
String result56;
String result57;
String result58;
String result59;
String result60;
String result61;
String result62;
String result63;
String result64;
String result65;
String result66;
String result67;
String result68;
String result69;
String result70;
String result71;
String result72;
String result73;
String result74;
String result75;
String result76;
String result77;
String result78;
String result79;
String result80;
String result81;
String result82;
String result83;
String result84;
String result85;
String result86;
String result87;
String result88;
String result89;
String result90;
String result91;
String result92;
String result93;
String result94;
String result95;
String result96;
String result97;
String result98;
String result99;
String result100;
String result101;
String result102;
String result103;
String result104;
String result105;
String result106;
String result107;
String result108;
String result109;
String result110;
String result111;
String result112;
String result113;
String result114;
String result115;
String result116;
String result117;
String result118;
String result119;
String result120;
String result121;
String result122;
String result123;
String result124;
String result125;
String result126;
String result127;
String result128;
String result129;
String result130;
String result131;
String result132;
String result133;
String result134;
String result135;
String result136;
String result137;
String result138;
String result139;
String result140;
String result141;
String result142;
String result143;
String result144;
String result145;
String result146;
String result147;
String result148;
String result149;
String result150;
String result151;
String result152;
String result153;
String result154;
String result155;
String result156;
String result157;
String result158;
String result159;
String result160;
String result161;
String result162;
String result163;
String result164;
String result165;
String result166;
String result167;
String result168;
String result169;
String result170;
String result171;
String result172;
String result173;
String result174;
String result175;
String result176;
String result177;
String result178;
String result179;
String result180;
String result181;
String result182;
String result183;
String result184;
String result185;
String result186;
String result187;
String result188;
String result189;
String result190;
String result191;
String result192;
String result193;
String result194;
String result195;
String result196;
String result197;
String result198;
String result199;
String result200;
String result201;
String result202;
String result203;
String result204;
String result205;
String result206;
String result207;
String result208;
String result209;
String result210;
String result211;
String result212;
String result213;
String result214;
String result215;
String result216;
String result217;
String result218;
String result219;
String result220;
String result221;
String result222;
String result223;
String result224;
String result225;
String result226;
String result227;
String result228;
String result229;
String result230;
String result231;
String result232;
String result233;
String result234;
String result235;
String result236;
String result237;
String result238;
String result239;
String result240;
String result241;
String result242;
String result243;
String result244;
String result245;
String result246;
String result247;
String result248;
String result249;
String result250;
String result251;
String result252;
String result253;
String result254;
String result255;
String result256;
String result257;
String result258;
String result259;
String result260;
String result261;
String result262;
String result263;
String result264;
String result265;
String result266;
String result267;
String result268;
String result269;
String result270;
String result271;
String result272;
String[] resultsizq = {result1, result3, result5, result7, result9, result11, result13, result15, result17, result19, result21, result23, result25, result27, result29, result31, result33, result35, result37, result39, result41, result43, result45, result47, result49, result51, result53, result55, result57, result59, result61, result63, result65, result67, result69, result71, result73, result75, result77, result79, result81, result83, result85, result87, result89, result91, result93, result95, result97, result99, result101, result103, result105, result107, result109, result111, result113, result115, result117, result119, result121, result123, result125, result127, result129, result131, result133, result135, result137, result139, result141, result143, result145, result147, result149, result151, result153, result155, result157, result159, result161, result163, result165, result167, result169, result171, result173, result175, result177, result179, result181, result183, result185, result187, result189, result191, result193, result195, result197, result199, result201, result203, result205, result207, result209, result211, result213, result215, result217, result219, result221, result223, result225, result227, result229, result231, result233, result235, result237, result239, result241, result243, result245, result247, result249, result251, result253, result255, result257, result259, result261, result263, result265, result267, result269, result271};
String[] resultsder = {result2, result4, result6, result8, result10, result12, result14, result16, result18, result20, result22, result24, result26, result28, result30, result32, result34, result36, result38, result40, result42, result44, result46, result48, result50, result52, result54, result56, result58, result60, result62, result64, result66, result68, result70, result72, result74, result76, result78, result80, result82, result84, result86, result88, result90, result92, result94, result96, result98, result100, result102, result104, result106, result108, result110, result112, result114, result116, result118, result120, result122, result124, result126, result128, result130, result132, result134, result136, result138, result140, result142, result144, result146, result148, result150, result152, result154, result156, result158, result160, result162, result164, result166, result168, result170, result172, result174, result176, result178, result180, result182, result184, result186, result188, result190, result192, result194, result196, result198, result200, result202, result204, result206, result208, result210, result212, result214, result216, result218, result220, result222, result224, result226, result228, result230, result232, result234, result236, result238, result240, result242, result244, result246, result248, result250, result252, result254, result256, result258, result260, result262, result264, result266, result268, result270, result272};
class CustomAdapter extends BaseAdapter{
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
view = getLayoutInflater().inflate(R.layout.customlayout,null);
TextView resultsiz= (TextView)view.findViewById(R.id.resultsiz);
TextView resultsde= (TextView)view.findViewById(R.id.resultsde);
resultsiz.setText(resultsizq[position]);
resultsde.setText(resultsder[position]);
return view;
}
}
@Override
protected Void doInBackground(Void... params) {
try {
Document doc = Jsoup.connect("http://www.rfebs.es/estadisticas/2017/liga/B_res.php").get();
Elements result = doc.getElementsByClass("bianco_me");
if (result.size() > 0) {
// get value
result1 = result.get(1).text();
result2 = result.get(2).text();
result3 = result.get(3).text();
result4 = result.get(4).text();
result5 = result.get(5).text();
result6 = result.get(6).text();
result7 = result.get(7).text();
result8 = result.get(8).text();
result9 = result.get(9).text();
result10 = result.get(10).text();
result11 = result.get(11).text();
result12 = result.get(12).text();
result13 = result.get(13).text();
result14 = result.get(14).text();
result15 = result.get(15).text();
result16 = result.get(16).text();
result17 = result.get(17).text();
result18 = result.get(18).text();
result19 = result.get(19).text();
result20 = result.get(20).text();
result21 = result.get(21).text();
result22 = result.get(22).text();
result23 = result.get(23).text();
result24 = result.get(24).text();
result25 = result.get(25).text();
result26 = result.get(26).text();
result27 = result.get(27).text();
result28 = result.get(28).text();
result29 = result.get(29).text();
result30 = result.get(30).text();
result31 = result.get(31).text();
result32 = result.get(32).text();
result33 = result.get(33).text();
result34 = result.get(34).text();
result35 = result.get(35).text();
result36 = result.get(36).text();
result37 = result.get(37).text();
result38 = result.get(38).text();
result39 = result.get(39).text();
result40 = result.get(40).text();
result41 = result.get(41).text();
result42 = result.get(42).text();
result43 = result.get(43).text();
result44= result.get(44).text();
result45= result.get(45).text();
result46= result.get(46).text();
result47= result.get(47).text();
result48= result.get(48).text();
result49= result.get(49).text();
result50= result.get(50).text();
result51= result.get(51).text();
result52= result.get(52).text();
result53= result.get(53).text();
result54= result.get(54).text();
result55= result.get(55).text();
result56= result.get(56).text();
result57= result.get(57).text();
result58= result.get(58).text();
result59= result.get(59).text();
result60= result.get(60).text();
result61= result.get(61).text();
...
result197= result.get(197).text();
result198= result.get(198).text();
result199= result.get(199).text();
result200 = result.get(200).text();
result201 = result.get(201).text();
result202 = result.get(202).text();
result203 = result.get(203).text();
result204 = result.get(204).text();
result205 = result.get(205).text();
result206 = result.get(206).text();
result207 = result.get(207).text();
result208 = result.get(208).text();
result209 = result.get(209).text();
result210 = result.get(210).text();
result211= result.get(211).text();
result212= result.get(212).text();
result213= result.get(213).text();
result214= result.get(214).text();
result215= result.get(215).text();
result216= result.get(216).text();
result217= result.get(217).text();
result218= result.get(218).text();
result219= result.get(219).text();
result220= result.get(220).text();
result221= result.get(221).text();
result222= result.get(222).text();
result223= result.get(223).text();
result224= result.get(224).text();
result225= result.get(225).text();
result226= result.get(226).text();
result227= result.get(227).text();
result228= result.get(228).text();
result229= result.get(229).text();
result230= result.get(230).text();
result231= result.get(231).text();
result232= result.get(232).text();
result233= result.get(233).text();
result234= result.get(234).text();
result235= result.get(235).text();
result236= result.get(236).text();
result237= result.get(237).text();
result238= result.get(238).text();
result239= result.get(239).text();
result240= result.get(240).text();
result241= result.get(241).text();
result242= result.get(242).text();
result243= result.get(243).text();
result244= result.get(244).text();
result245= result.get(245).text();
result246= result.get(246).text();
result247= result.get(247).text();
result248= result.get(248).text();
result249= result.get(249).text();
result250= result.get(250).text();
result251= result.get(251).text();
result252= result.get(252).text();
result253= result.get(253).text();
result254= result.get(254).text();
result255= result.get(255).text();
result256= result.get(256).text();
result257= result.get(257).text();
result258= result.get(258).text();
result259= result.get(259).text();
result260= result.get(260).text();
result261= result.get(261).text();
result262= result.get(262).text();
result263= result.get(263).text();
result264= result.get(264).text();
result265= result.get(265).text();
result266= result.get(266).text();
result267= result.get(267).text();
result268= result.get(268).text();
result269= result.get(269).text();
result270= result.get(270).text();
result271= result.get(271).text();
result272= result.get(272).text();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
调试器:
03/26 18:27:32: Launching app
$ adb push /Users/davidgarciaballester/AndroidStudioProjects/BaseballSpain/app/build/outputs/apk/app-debug.apk /data/local/tmp/davidgb.baseballspain
$ adb shell pm install -r "/data/local/tmp/davidgb.baseballspain"
Success
$ adb shell am start -n "davidgb.baseballspain/davidgb.baseballspain.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Waiting for application to come online: davidgb.baseballspain.test | davidgb.baseballspain
Connecting to davidgb.baseballspain
Connected to the target VM, address: 'localhost:8621', transport: 'socket'
I/art: Not late-enabling -Xcheck:jni (already on)
W/art: Unexpected CPU variant for X86 using defaults: x86_64
W/ActivityThread: Application davidgb.baseballspain is waiting for the debugger on port 8100...
I/System.out: Sending WAIT chunk
I/art: Debugger is active
I/System.out: Debugger has connected
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1465)
W/System: ClassLoader referenced unknown path: /data/app/davidgb.baseballspain-1/lib/x86_64
I/InstantRun: Instant Run Runtime started. Android package is davidgb.baseballspain, real application class is null.
I/art: Do partial code cache collection, code=28KB, data=22KB
I/art: After code cache collection, code=19KB, data=18KB
I/art: Increasing code cache capacity to 128KB
I/art: Do partial code cache collection, code=57KB, data=43KB
I/art: After code cache collection, code=52KB, data=41KB
I/art: Increasing code cache capacity to 256KB
W/System: ClassLoader referenced unknown path: /data/app/davidgb.baseballspain-1/lib/x86_64
D/AndroidRuntime: Shutting down VM
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: davidgb.baseballspain, PID: 2464
java.lang.RuntimeException: Unable to start activity ComponentInfo{davidgb.baseballspain/davidgb.baseballspain.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:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
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 davidgb.baseballspain.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Disconnected from the target VM, address: 'localhost:8621', transport: 'socket'
编辑:app没有显示任何内容,因为我没有在onCreate中指定活动布局。
答案 0 :(得分:3)
由于您未在Button but1 = (Button) findViewById(R.id.but);
onCreate
之前指定活动布局,因此发生了崩溃。
示例:setContentView(R.layout.activity_main);
希望这有帮助。
您的自定义适配器似乎不起作用,在获取项目总数时返回静态0 ..等等
@Override
public int getCount() {
return 0;
}