JAdroid 2017-03-05 15:31 采纳率: 0%
浏览 1973

android adapter提示空指针异常情况

主要代码如下:
package com.example.mydreamhero.ActivityManage;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Bundle;
import android.os.Debug;
import android.widget.ListView;

import com.example.mydreamhero.R;

import java.util.ArrayList;
import java.util.List;

/**

  • Created by 刘 on 2017/3/5. */

public class AMProcessActivity extends Activity {
private ListView listView;
private List mAmprocessInfo = null;
private ActivityManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.am_main);
listView = (ListView) findViewById(R.id.listview_am_process);
manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
//提示下面这一行出错
listView.setAdapter(new AMProcessAdapter(this, getRunningprocess()));
}

private List<AMProcessInfo> getRunningprocess() {
    mAmprocessInfo = new ArrayList<AMProcessInfo>();
    List<ActivityManager.RunningAppProcessInfo> appprocessInfo =
            manager.getRunningAppProcesses();
    for (int i = 0; i < appprocessInfo.size(); i++) {
        ActivityManager.RunningAppProcessInfo info =
                appprocessInfo.get(i);
        int pid = info.pid;
        int uid = info.uid;
        String processName = info.processName;
        int[] memorypid = new int[]{pid};
        Debug.MemoryInfo memoryInfo[] = manager.getProcessMemoryInfo(memorypid);
        int memorySize = memoryInfo[0].getTotalPss();

        AMProcessInfo amProcessInfo = new AMProcessInfo();
        amProcessInfo.setPid(""+pid);
        amProcessInfo.setUid(""+uid);
        amProcessInfo.setMemorySize(memorySize+"KB");
        amProcessInfo.setProcessName(processName);
        mAmprocessInfo.add(amProcessInfo);
    }
    return mAmprocessInfo;
}

}**

出错一次如下:
03/05 23:17:17: Launching app
$ adb push F:\androidforGoogle\MyDreamHero\app\build\outputs\apk\app-debug.apk /data/local/tmp/com.example.mydreamhero
$ adb shell pm install -r "/data/local/tmp/com.example.mydreamhero"
pkg: /data/local/tmp/com.example.mydreamhero
Success

$ adb shell am start -n "com.example.mydreamhero/com.example.mydreamhero.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Waiting for process to come online
Connected to process 3275 on device genymotion-genymotion_vbox86p_6_0_160825_141918-192.168.196.101:5555
I/art: Background partial concurrent mark sweep GC freed 1613(300KB) AllocSpace objects, 0(0B) LOS objects, 39% free, 1587KB/2MB, paused 101us total 183.475ms
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mydreamhero, PID: 3275
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mydreamhero/com.example.mydreamhero.ActivityManage.AMProcessActivity}: 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.example.mydreamhero.ActivityManage.AMProcessActivity.onCreate(AMProcessActivity.java:29)
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) 
我查了好多次都没有找到原因,求大神来不吝解答一下

  • 写回答

3条回答 默认 最新

  • devmiao 2017-03-05 15:28
    关注
    评论

报告相同问题?

悬赏问题

  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。