Android:无法启动java.lang.nullpointerexception活动

时间:2012-05-08 06:21:34

标签: android android-activity

我创建了一个activity并在onCreate()函数中编写了一些函数。 在运行应用程序时,我收到一些错误消息无法启动活动java.lang.nullpointerexception。我已在AndroidManifest.xml

中包含此活动
@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_report_hr_full);
         List<Employee> data = new ArrayList<Employee>();
            Employee eg=new Employee();
            ListView li=(ListView)findViewById(R.id.user_report_hr_list);
            Intent myIntent = getIntent(); // this is just for example purpose
            Bundle b=myIntent.getExtras();
            Log.i("User Report by Hours", b.toString());
            Toast.makeText(getApplicationContext(), "User ", Toast.LENGTH_LONG).show();
            if(b !=null)
            {
                String username = b.getString("username");
                String userid = b.getString("userid");
                String startdate = b.getString("startdate");
                String enddate = b.getString("enddate");
                String mode=b.getString("mode");
                Log.i("LOOP", userid+"-"+startdate+"-"+enddate+"-"+mode);
                data=eg.getReports(username, userid, startdate, enddate,mode);
                Log.i("Get Report",data.toString());
                li.setAdapter(new UserHoursByReportArrayadapter(getApplicationContext(),data));
            }


    }

我的日志文件是

05-08 11:45:30.745: I/User Report by HoursActivity:mode(517): 1
05-08 11:45:30.837: I/User Report by Hours(517): Bundle[mParcelledData.dataSize=236]
05-08 11:45:30.845: I/LOOP(517): 1-2012-04-08-2012-05-08-1
05-08 11:45:30.855: D/HTTP(517): Execute HTTP Post Request
05-08 11:45:30.855: D/UserID(517): Userid in EmpGlobal [1]
05-08 11:45:30.855: D/Start date(517): Start date in EmpGlobal [2012-04-08]
05-08 11:45:30.855: D/End date(517): End date in EmpGlobal [2012-05-08]
05-08 11:45:30.855: D/Mode(517): Mode in EmpGlobal [1]
05-08 11:45:48.275: I/log_tag(517):  Time: 2033755
05-08 11:45:48.275: I/log_tag(517):  project name: AndroidTimesheet
05-08 11:45:48.285: I/log_tag(517):  username: admin1
05-08 11:45:48.285: I/log_tag(517):  Time: 34692
05-08 11:45:48.285: I/log_tag(517):  project name: Google
05-08 11:45:48.285: I/log_tag(517):  username: admin1
05-08 11:45:48.285: I/Get Report(517): [com.technogems.employeetimecard.EmployeeGlobal@44da2ca8, com.technogems.employeetimecard.EmployeeGlobal@44da2f88]
05-08 11:45:48.285: I/User Report by HoursAdapter(517): Inside constructor
05-08 11:45:48.285: D/AndroidRuntime(517): Shutting down VM
05-08 11:45:48.285: W/dalvikvm(517): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
05-08 11:45:48.285: E/AndroidRuntime(517): Uncaught handler: thread main exiting due to uncaught exception
05-08 11:45:48.295: E/AndroidRuntime(517): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.technogems.employeetimecard/com.technogems.employeetimecard.UserHoursByReport}: java.lang.NullPointerException
05-08 11:45:48.295: E/AndroidRuntime(517):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
05-08 11:45:48.295: E/AndroidRuntime(517):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-08 11:45:48.295: E/AndroidRuntime(517):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-08 11:45:48.295: E/AndroidRuntime(517):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-08 11:45:48.295: E/AndroidRuntime(517):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 11:45:48.295: E/AndroidRuntime(517):  at android.os.Looper.loop(Looper.java:123)
05-08 11:45:48.295: E/AndroidRuntime(517):  at android.app.ActivityThread.main(ActivityThread.java:4363)
05-08 11:45:48.295: E/AndroidRuntime(517):  at java.lang.reflect.Method.invokeNative(Native Method)
05-08 11:45:48.295: E/AndroidRuntime(517):  at java.lang.reflect.Method.invoke(Method.java:521)
05-08 11:45:48.295: E/AndroidRuntime(517):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-08 11:45:48.295: E/AndroidRuntime(517):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-08 11:45:48.295: E/AndroidRuntime(517):  at dalvik.system.NativeStart.main(Native Method)
05-08 11:45:48.295: E/AndroidRuntime(517): Caused by: java.lang.NullPointerException
05-08 11:45:48.295: E/AndroidRuntime(517):  at com.technogems.employeetimecard.UserHoursByReport.onCreate(UserHoursByReport.java:35)
05-08 11:45:48.295: E/AndroidRuntime(517):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-08 11:45:48.295: E/AndroidRuntime(517):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
05-08 11:45:48.295: E/AndroidRuntime(517):  ... 11 more

请为我提供开始该活动的正确方法

1 个答案:

答案 0 :(得分:0)

删除此行: Log.i(“按小时划分的用户报告”,b.toString()); 其中b为null所以发生了nullpointerException