Android中的我的应用程序包含超过7个活动第一个活动是启动画面,然后启动主要活动2其他活动包含选项卡,当我尝试通过按钮调用主要活动2中的选项卡活动应用程序崩溃时,我更改选项卡活动列出活动时,我尝试再次调用列表活动同样的错误。 请有人帮帮我吗? BR。
这是第一个启动页面
package com.almadar.evo.app.evo_app;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
ProgressBar pgr;
int Progress=0;
Handler h=new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pgr =(ProgressBar) findViewById(R.id.progressBar2);
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
Progress += 20;
h.post(new Runnable() {
@Override
public void run() {
pgr.setProgress(Progress);
if (Progress == pgr.getMax()) {
//pgr.setVisibility(4);
Intent in= new Intent(getApplicationContext(),Main2Activity.class);
startActivity(in);
finish();
}
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// e.printStackTrace();
}
}
}
}).start();
}
@Override
protected void onStop() {
super.onStop();
}
}
第二页
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void bupostclick(View view) {
Intent in= new Intent(getApplicationContext(),postpaid1.class);
startActivity(in);
}
public void bupreclick(View view) {
Intent in1= new Intent(getApplicationContext(),pre1.class);
startActivity(in1);
}
}
这是第三页包含按钮,带我到列表视图页面
public class pre1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pre1);
}
public void home(View view) {
Intent in= new Intent(getApplicationContext(),Main2Activity.class);
startActivity(in);
finish();
}
public void mainrefill(View view) {
Intent intent1 = new Intent(pre1.this,refil3.class);
startActivity(intent1);
}
public void home1(View view) {
}
}
这是列表视图页面
public class pre2 extends Activity implements AdapterView.OnItemClickListener {
ListView l;
String[] service={"1","2","3","4","5"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l= (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,service);
l.setAdapter(adapter);
l.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int i, long l) {
TextView temp= (TextView)view;
Toast.makeText(this,temp.getText()+""+i,Toast.LENGTH_SHORT).show();
}
}
这是APP崩溃时的错误
--------- beginning of crash
05-04 09:57:23.290 12144-12144/com.almadar.evo.app.evo_app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.almadar.evo.app.evo_app, PID: 12144
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.almadar.evo.app.evo_app/com.almadar.evo.app.evo_app.refil3}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.almadar.evo.app.evo_app.refil3.onCreate(refil3.java:22)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
请粘贴崩溃报告,并且要使用ListActivity,您的布局上应该有一个列表视图,ID为android:id="@android:id/list"
答案 1 :(得分:0)
尝试使用prev2中的以下代码
l= new ListView(this);
setContentView(R.layout.l);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,service);
l.setAdapter(adapter);
l.setOnItemClickListener(this);